Cycling is one of the basic concepts of programming languages. When you want to run a series of commands repeatedly until certain conditions are met, the loop is very convenient.
Such as scripting languages like Bash, the cycle is useful to automate repetitive tasks. In Bash script has three basic cyclic structure, for loop , while loop, an until loop .
This tutorial explains the basics of Bash while loop, and for changing the break and continue statements circulation flow.
Bash while loop
As long as a calculation result given condition is true, while loop will be executed using an unknown number of times a given set of commands.
Bash while loop takes the following form:
while [CONDITION] do [COMMANDS] done
Assess the conditions before executing the command. If the condition evaluates to true, then execute the command. Otherwise, if the condition evaluates to false, then the loop will terminate, program control passes to the subsequent commands.
In the following example, at each iteration, the current value of the loop variable print and variable i is incremented by 1.
i=0 while [ $i -le 2 ] do echo Number: $i ((i++)) done
As long as i is less than or equal to 2, Tue will loop iteration. It produces the following output:
Number: 0 Number: 1 Number: 2
Infinite while loop
Infinite loop is an infinite repetition and never-ending cycle. If the condition always evaluates to true, you'll get an infinite loop.
In the following example, we use the built-in commands: always returns true command to create an infinite loop. You can also use the built-in commands true or consistently return true of any other statement.
while : do echo "Press <CTRL+C> to exit." sleep 1 done
The above while loop will run indefinitely. You can press down and terminate the cycle CTRL + C.
This is the equivalent of a single line:
while :; do echo 'Press <CTRL+C> to exit.'; sleep 1; done
Read the file line by line
One of the most common use of the while loop is read line by line file, data stream or variable.
In the following example, while circulating the / etc / passwd file and read line by line to print each line.
file=/etc/passwd while read -r line; do echo $line done < "$file"
We use input redirection (< "$ file") to transfer the file to the read cycle control commands, rather than using conditional control while loop. while loop will run until the last line read.
When reading the lines in the file line by line always read and use the -r option to prevent the backslash as an escape character.
By default, the read command to trim leading / trailing whitespace characters (spaces and tabs). Use the command options IFS = read before can prevent this behavior
file=/etc/passwd while IFS= read -r line; do echo $line done < "$file"
break and continue statements
break and continue statements can be used to control the while loop.
break statement
break statement terminates the current program loop and passes control back to the command to terminate the cycle. It is typically used to terminate the loop when certain conditions are met.
In the following example, once the item is equal to the current iteration, interrupt the execution of the loop 2.
i=0 while [ $i -lt 5 ] do echo "Number: $i" ((i++)) if [[ "$i" == '2' ]]; then break fi done echo 'All Done!'
Number: 0 Number: 1 All Done!
continue Statement
continue statement exits the current iteration of the loop, and program control passes to the next loop iteration.
In the following sections, once the item is equal to 2continue statements in the current iteration, it would lead to execution returns to the beginning of the loop and continue to the next iteration.
i=0 while [ $i -lt 5 ] do ((i++)) if [[ "$i" == '2' ]]; then continue fi echo "Number: $i" done echo 'All Done!'
Number: 1 Number: 3 Number: 4 Number: 5 All Done!
in conclusion
By now, you should have a good understanding of how to use bash while loop.
If you have any questions, please feel free to comment.