Bash Loops - Execute it again and again...

Loops are one of the fundamental concepts of programming languages. Loops are handy when you want to run a series of commands a number of times until a particular condition is met. Loops are helpful for automating repetitive tasks in scripting languages like Bash. For loop, while loop, and until loop are the three fundamental loop building blocks in Bash scripting.



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 [ <some test> ]
do
  <commands>
done

In the example below, on each iteration, the current value of the variable i is printed and increased by one (I++).

#!/bin/bash

i=0
while [ $i -le 10 ]
do
 echo The current Number: $i
 ((i++))
done

Output:

[root@scriptbox scripts]# vim loop.sh
[root@scriptbox scripts]# chmod +x loop.sh
[root@scriptbox scripts]# vim loop.sh
[root@scriptbox scripts]# ./loop.sh
The current Number: 0
The current Number: 1
The current Number: 2
The current Number: 3
The current Number: 4
The current Number: 5
The current Number: 6
The current Number: 7
The current Number: 8
The current Number: 9
The current Number: 10

Infinite while Loop

A loop that never ends and keeps repeating forever is known as an infinite loop. You get an infinite loop if the condition always evaluates to true. In the following example, we are using the built-in command: to create an infinite loop. :

while :
do
  echo "Press <CTRL+C> to exit."
  sleep 1
done

The while loop above will run indefinitely. You can terminate the loop by pressing CTRL+C.



Break and Continue Statements

The break statement ends the current loop and transfers control to the command that comes after the loop that was terminated. When a particular condition is satisfied, it is typically used to end the loop. When the current item being iterated reaches the value 10 in the example below, the loop will stop running.

i=0

while [ $i -lt 10 ]
do
  echo "Number: $i"
  ((i++))
  if [[ "$i" == '10' ]]; then
    break
  fi
done

echo 'Done'

The continue command ends the current iteration of a loop and transfers control to the loop's subsequent iteration. The continue statement will cause execution to resume at the start of the loop and move on to the next iteration in the example below once the current iterated item equals 3.

i=0

while [ $i -lt 5 ]
do
  ((i++))
  if [[ "$i" == '3' ]]; then
    continue
  fi
  echo "Number: $i"
done

echo 'Done'


For loop

Like any other programming language, bash shell scripting also supports 'for loops' to perform repetitive tasks. It helps us to iterate a particular set of statements over a series of words in a string, or elements in an array. We can apply 'for loop' on bash script in two ways. One way is 'for-in' and another way is the c-style syntax. Following is the syntax of 'for loop' in bash shell scripting:

for variable in list  
do  
commands  
done

Or

for (( expression1; expression2; expression3 ))  
do  
commands  
done  

The "for loop" statement has the following important points:

  1. In Bash, the "do" keyword precedes each block of a "for loop" and is followed by the commands inside the block. The "done" keyword ends the "for loop" statement.

  2. The declared list variables determine how many times a "for loop" will iterate.

  3. One item will be chosen at random from the list by the loop, and its value will be assigned to a variable that will be used during the loop.

  4. The loop returns to the top, chooses the next item from the list, and repeats the entire process after the commands between "do" and "done" have been executed.

  5. The list may contain, space-separated, strings of numbers or other data.



Basic 'For Loop' Example:

   #!/bin/bash
    #This is the basic example of 'for loop'.

    Blog="WWW.AgileQualityBlog"

    for learn in $Blog
    do
    echo $Blog
    done

    echo "Thanks for learning "

Output:

[root@scriptbox scripts]# ./for_loop.sh
WWW.AgileQualityBlog
Thanks for learning

And here is another one:

#!/bin/bash

for Names in Rocky1 Rocky2 Rocky3 Rocky4 Rocky5
do
  echo "Looping....."
  sleep 1
  echo "###################################################"
  echo "Value of VAR1 is $Names."
  echo "###################################################"
  date
  echo
done

Output:

 
[root@scriptbox scripts]# ./for_loop.sh
Looping.....
###################################################
Value of VAR1 is Rocky1
###################################################
Sun Jun 26 14:23:56 UTC 2022
.
.
.
Looping.....
###################################################
Value of VAR1 is Rocky5
###################################################
Sun Jun 26 14:24:01 UTC 2022


1 view0 comments

Recent Posts

See All