How to make automation with bash for loop under Linux/UNIX operating system? How do I use break, continue and the parameter for loop control expression? How to act on files using for loop?
For loop is a very useful tool to solve many problems in the programming world and therefore we will solve some problems in the real world. In Linux we use loops via Bash, Python to make automation like password script, counting script. In this article, we will explain all of the kind of loops for Bash.
1) for loop
We can use For loop to read all elements in a list or part of them and displaying these elements on the screen.
Syntax of For loop
do
commands
done
We will make a script to send greeting message for each day of the week, so we will use for
loop to read each day and print it out. Create file welcome.sh
with nano
editor and paste code below.
#!/bin/bash for day in Sunday Saturday Monday Tuesday Wednesday Thursday Friday do echo "Welcome $day day" done
Run the welcome.sh
file with bash
command.
$ bash welcome.sh
output Welcome Sunday day Welcome Saturday day Welcome Monday day Welcome Tuesday day Welcome Wednesday day Welcome Thursday day Welcome Friday day
The for loop will take each item in the list respectively, and assign this item to the variable $day
, after that execute the code between do
and done
then go back to the top, assign the next item in the list and repeat over.
We can use a range with for loop to put start point and end point.
#!/bin/bash for i in {10..20} do echo "Welcome $i times" done
$ bash range.sh
output Welcome 10 times Welcome 11 times Welcome 12 times Welcome 13 times Welcome 14 times Welcome 15 times Welcome 16 times Welcome 17 times Welcome 18 times Welcome 19 times Welcome 20 times
If we want to count even number from 1 to 20, we will add step for range.
#!/bin/bash for i in {10..20..2} do echo "Welcome $i times" done
$ bash even.sh
output Welcome 10 times Welcome 12 times Welcome 14 times Welcome 16 times Welcome 18 times Welcome 20 times
Another type of using step with for loop.
#!/bin/bash for ((i=1;i<=25;i+=5)) do echo $i done
$ bash step5.sh
output 1 6 11 16 21
And for negative step we can use the following type.
#!/bin/bash for ((i=25;i>=1;i-=5)) do echo $i done
$ bash step-5.sh
output 25 20 15 10 5
Examples on For loop
1) Acting on files using for loop
Bash For loop is the best way when we are working on files.
#!/bin/bash for file in ~/*.txt do echo $file done
$ bash find_txt.sh
output /root/cat.txt /root/echo.txt /root/file.txt /root/f.txt /root/nano.txt /root/printf.txt
2) One line For loop
we can execute For loop in one line, we will rename all *.txt
files to remove the file extension.
$ for filename in *.txt; do mv "$filename" "${filename%.txt}"; done
Or in script
!#/bin/bash for filename in *.txt do mv "$filename" "${filename%.txt}" done
3) Reading Command-line arguments
When we are executing For loop script, we can enter arguments.
for myvalue in $* do echo "Argument: $myvalue" done
$ bash linoxide.sh I Love LinOxide
output Argument: I Argument: Love Argument: LinOxide
4) Reading odd and even number
We will write script to read the even and the odd numbers.
for (( n=10; n<=15; n++ )) do if (( $n%2==0 )) then echo "$n is even" else echo "$n is odd" fi done
$ bash even_odd.sh
output 10 is even 11 is odd 12 is even 13 is odd 14 is even 15 is odd
2) while loop
While loop depend on the condition is true, if the condition is false the interpreter get out from the loop.
Syntax of while loop
<commands>
done
We will count from 10 to 20 and print out the results. So we will put a condition that the counter less than or equal 20.
#!/bin/bash # Basic while loop counter=10 while [ $counter -le 20 ] do echo Number : $counter ((counter++)) done
$ bash while.sh
output Number : 10 Number : 11 Number : 12 Number : 13 Number : 14 Number : 15 Number : 16 Number : 17 Number : 18 Number : 19 Number : 20
3) Until loop
Until loop like while loop but the interpreter excute the commands within it until the condition becomes true.
Syntax of until loop
<commands>
done
We will count from 10 to 20 and print out the results. So we will put a condition that the counter greater than or equal 20.
#!/bin/bash # Basic Until loop counter=10 until [ $counter -gt 20 ] do echo Number : $counter ((counter++)) done
$ bash until.sh
output Number : 10 Number : 11 Number : 12 Number : 13 Number : 14 Number : 15 Number : 16 Number : 17 Number : 18 Number : 19 Number : 20
4) Controlling loops
We can use Break
or Continue
to control loops.
Break statement
for Break
statement, we can get out from the loop and no need to complete the loop when we use if
statement inside the loop.
#!/bin/bash # Basic loop use break counter=10 until [ $counter -gt 20 ] do echo Number : $counter if [ $counter -eq 15 ] then echo Done break fi ((counter++)) done
$ bash break.sh
output Number : 10 Number : 11 Number : 12 Number : 13 Number : 14 Number : 15 Done
Continue statement
For Continue
statement, we can go on the loop and no need to finish the loop when we are using if
statement inside the loop.
#!/bin/sh NUMS="1 2 3 4 5 6 7" for NUM in $NUMS do Q=`expr $NUM % 2` if [ $Q -eq 0 ] then echo "A number is an even number!!" continue fi echo "Found odd number" done
$ bash continue.sh
output Found odd number A number is an even number!! Found odd number A number is an even number!! Found odd number A number is an even number!! Found odd number
5) Select loop
Select loop like while and until loop but allows you to create a simple menu system.
Syntax of select loop
do
<commands>
done
We will make three options and use select
loop to choose from them.
#!/bin/bash # select script PS3='Please enter your choice: ' options=("Option 1" "Option 2" "Option 3" "Quit") select opt in "${options[@]}" do case $opt in "Option 1") echo "you choose choice 1" ;; "Option 2") echo "you choose choice 2" ;; "Option 3") echo "you choose choice $REPLY which is $opt" ;; "Quit") break ;; *) echo "invalid option $REPLY";; esac done
$ bash select.sh
output 1) Option 1 2) Option 2 3) Option 3 4) Quit Please enter your choice: 1 you chose choice 1 Please enter your choice: 5 invalid option 5 Please enter your choice: 4
Conclusion
- Bash For loop used in synchronization, making password, backup and etc...
- Do while is same as while but the interpreter executes the first code without any conditions
- Break statement is very important for getting out from the loop
- Select statement is useful when we have many options