Skip to main content

Posts

Showing posts with the label for

loops in bash shell- for , while and until

Loops allow us to take a series of commands and keep re-running them until a particular situation is reached. They are useful for automating repetitive tasks. There are 3 basic loop structures in Bash scripting which we'll look at below. There are also a few statements which we can use to control the loops operation. while loop One of the easiest loops to work with is while loops. They say, while an expression is true, keep executing these lines of code. They have the following format: while [ test condition ] do #statements done Example: print all odd numbers less than 50 i=1 while [ $i -le 50 ] do echo $i i=`expr $i + 2` done until loop The until loop is fairly similar to the while loop. The difference is that it will execute the commands within it until the test becomes true.  Syntax until [ test condition ] do #statements done Example: print all odd numbers less than 50 i=1 until [ $i -ge 50 ] do echo $i i=`expr $i + 2` done for loop The for loo...