Shell Scripting – Loops
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed shell operators.
https://cloudaffaire.com/shell-scripting-operators/
In this blog post, we will discuss loops. Like any other programming language, bash also supports loop. A loop in a nut shell is a way to repeat a command until a certain condition is met. Bash supports while, for, until and select loops.
Shell Scripting – Loops
While Loop:
The while construct allows for repetitive execution of a list of commands, as long as the condition is true. Once the condition is false, while loop stop execution.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
############################# ## Shell Scripting | Loops ## ############################# ## Prerequisites: One Unix/Linux/POSIX-compliant operating system with bash shell ## ----------- ## While Loops ## ----------- ## while condition ## do ## command ## done ## basic while loop i=1 while [ $i -le 5 ] do echo "$i" ((i++)) ## increments i by one each step done i=1; while [ $i -le 5 ]; do echo "$i"; ((i++)); done ## same thing in one line ## infinite while loop while : ## using colon do echo "infinite loop, hit control + c to stop" done while true ## using true do echo "infinite loop, hit control + c to stop" done ## nested while loop i=1;j=5 while [ $i -le 5 ] do while [ $j -ge 1 ] do echo $j ((j--)) ## decrements j by 1 in each step done j=5 ## reset j to 5 again ((i++)) ## increments i by 1 in each step done ## while loop with break and continue i=1 while [ $i -le 5 ] do echo $i ((i++)) if [ $i -eq 4 ]; then break ## stops at 4 fi done i=0 while [ $i -lt 5 ] do ((i++)) if [ $i -eq 2 ]; then continue ## skips 2 fi echo $i done ## while loop with redirection myfile=/etc/passwd while read -r line; do echo "line: "$line ## reads and outputs /etc/passwd line by line done < "$myfile" |
For Loop:
The for construct allowed for iterate over a list. As long as it finds an item in the list, for loop continue to iterate over the list and stops iteration once it reaches to the end of the list.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
## --------- ## For Loops ## --------- ## for var in list ## do ## command ## done ## Basic for loops for i in 1 2 3 4 5 do echo $i ## returns 1 2 3 4 5 done for i in {1..5} do echo $i ## returns 1 2 3 4 5 done for i in {1..10..2} do echo $i ## returns 1 3 5 7 9 done for i in {1..10..2}; do echo $i; done ## same thing in one line for i in $(seq 1 2 10) ## same thing using seq do echo $i ## returns 1 3 5 7 9 done ## C style for loop for (( i=1; i<=5; i++ )) do echo $i ## returns 1 2 3 4 5 done ## infinite for loops for (( ; ; )) do echo "infinite loop, hit control + c to stop" done ## for loop to itterate an array a=(1 2 3 4 5) ## declare an array for i in ${a[@]} do echo $i ## returns 1 2 3 4 5 done ## nested for loops for i in {1..5..1} do for j in {5..1..1} do printf "$i $j\n" done done ## for loop with break and continue for i in 1 2 3 4 5 do if [ $i -eq 4 ] then break ## stops at 4 fi echo $i ## returns 1 2 3 done for i in 1 2 3 4 5 do if [ $i -eq 4 ] then continue ## skips at 4 fi echo $i ## returns 1 2 3 5 done ## for loop with output of other commands for i in `ls /` ## for all items in root directory do echo $i done for i in $(ls /) ## for all items in root directory do echo $i done |
Until Loop:
The until loop is almost equal to the while loop, except that the code is executed while the condition evaluates to false.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
## ----------- ## Until Loops ## ----------- ## until condition ## do ## command ## done ## basic until loop i=1 until [ $i -gt 5 ] do echo $i ## returns 1 2 3 4 5 ((i++)) done ## infinite until loop i=1 until [ $i -eq 0 ] do echo "infinite loop, hit control + c to stop" done ## until with break and continue i=1 until [ $i -gt 5 ] do echo $i ## returns 1 2 3 ((i++)) if [ $i -eq 4 ] then break ## stops at 4 fi done i=0 until [ $i -gt 4 ] do ((i++)) if [ $i -eq 2 ]; then continue ## skips 2 fi echo $i ## returns 1 3 4 5 done ## until command with other commands until cat mymissingfile do echo "waiting for mymissingfile" sleep 5 echo "hello world" > mymissingfile ## create the missing file echo "mymissingfile now exist" done |
Select Loop:
The select construct generates a menu from the list of items that are passed into it. The select command is often used with switch-case statements to generate a menu in bash.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
## ----------- ## Select Loop ## ----------- ## select var in list ## do ## command ## done PS3="Select a game (1-4): " ## sets PS3 select i in criket football basketball exit do case $i in criket) echo "You have selected Criket";; football) echo "You have selected Football";; basketball) echo "You have selected Basketball";; exit) break;; esac done unset PS3 |
Hope you have enjoyed this article, in the next blog post we will discuss conditions in bash.