Loop Control in Shell Script - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Random Posts

Loop Control in Shell Script

Share This

Most languages have the concept of loops. If we want to perform a task repetitively with a little bit of change, the loop is the solution to write the logic. Different types of loops are used in this scripting language. The following examples demonstrate the loops with some examples.

for loop

There are two types of syntaxes of for loop. In the first example, the loop is iterated based on the value of i. The first part is an initialization, the second part is condition checking and the last part is increment or decrement.

#!/bin/bash for (( i=1; i<=5; i++ )) do echo "$i" done echo $x

In the following example, the loop iterates over the elements starting from 0 to 5. In the first iteration, i will be assigned 0, in the next iteration i will be 1, and so on.


#!/bin/bash for i in 0 1 2 3 4 5 do echo "$i" done echo $x

nested for loop

Similar to the if-else block, for loop can also be nested as shown below.


#!/bin/bash echo "Stars" for (( i=1; i<=5; i++ )) do for (( j=1; j<=i; j++ )) do echo -n " *" done echo "" done

while loop

Any one of for or while loops can be used to perform repetitive tasks. The syntax of the while loop is shown in the following example.


#!/bin/bash i=5 while test $i != 0 do echo "$i" i=`expr $i - 1` done

nested while loop

Similar to for loop nesting, a while loop can also be nested as shown below.


#!/bin/bash x=0 while [ $x -lt 10 ] ; do y=$x while [ $y -ge 0 ] ; do echo "$y \c" y=`expr $y - 1` done x=`expr $x + 1` echo $x done

until loop

There is another loop, until, which can be used instead of the while loop. But the difference is that the loop will run until the condition is true, whereas the while loop runs until the condition is false.


#!/bin/bash x=1; until [ $x -ge 10 ] do echo $x x=`expr $x + 1` done

select loop



#!/bin/bash

break and continue statement

Script to find an average of numbers.

#!/bin/bash # avg=0 temp_total=0 number_of_args=$# if [ $# -lt 2 ] ; then echo -e "Opps! I need at least 2 command line args\n" echo -e "Syntax: $0: number1 number2 ... numberN\n" echo -e "Example:$0 5 4\n\t$0 56 66 34" exit 1 fi for i in $* do temp_total=`expr $temp_total + $i` done avg=`expr $temp_total / $number_of_args` echo "Average is $avg"

The sum of digits of a number

#!/bin/bash if [ $# -ne 1 ] then echo "Usage: $0 number" echo " I will find the sum of all digits for given number" echo " For eg. $0 123, I will print 6 as sum of all digit (1+2+3)" exit 1 fi n=$1 sum=0 sd=0 while [ $n -gt 0 ] do sd=`expr $n % 10` sum=`expr $sum + $sd` n=`expr $n / 10` done echo "Sum of digit for number is $sum"

Factorial of a Number using Shell Script

#!/bin/bash n=0 on=0 fact=1 echo -n "Enter a number to find factorial: " read n on=$n while [ $n -ge 1 ] do fact=`expr $fact \* $n` n=`expr $n - 1` done echo "Factorial for $on is $fact"

Reverse a Number using Shell Script

#!/bin/bash if [ $# -ne 1 ] then echo "Usage: $0 number" echo " Reseverse a number" echo " For eg. 1234, It will print 4321" exit 1 fi n=$1 rev=0 sd=0 while [ $n -gt 0 ] do sd=`expr $n % 10` rev=`expr $rev \* 10 + $sd` n=`expr $n / 10` done echo "Reverse number is $rev"

Program for Fibonacci Series

# Input for N read N # First Number of the Fibonacci Series a=0 # Second Number of the Fibonacci Series b=1 echo "The Fibonacci series is : " for (( i=0; i<N; i++ )) do echo -n "$a " fn=$((a + b)) a=$b b=$fn done # End of for loop

Armstrong number within a range

#!/bin/bash i=1 read n while((i-le$n)) do c=$i; d=$i; b=0; a=0 #checking each number while ((c>0)) do #separating each digit a=$((c%10)) #finding cube of each digit and adding them b=$((b + a*a*a)) c=$((c/10)) done if ((b==d)); then echo "$i" fi i=$((i+1)) done

Happy Exploring!

No comments:

Post a Comment