Programming Tutorials

for loop in Linux Shell Script

By: Dorris in Linux Tutorials on 2011-01-15  

Syntax:

      for { variable name } in { list }
            do
                execute one for each item in the list until the list is
                not finished (And repeat all statement between do and done)
            done

In order to understand the above syntax, let us try the following script in your shell:

$ cat > testfor
for i in 1 2 3 4 5
do
  echo "Welcome $i times"
done

Run the above script as follows:

$ chmod +x testfor
$ ./testfor

The for loop first creates i variable and assigned a number to i from the list of number from 1 to 5, The shell execute echo statement for each assignment of i. (This is usually know as iteration) This process will continue until all the items in the list were not finished, because of this it will repeat 5 echo statements. To make you idea more clear try following script:

$ cat > mtable
#!/bin/sh
#
#Script to test for loop
#
#
if [ $# -eq 0 ] then
  echo "Error - Number missing form command line argument"
  echo "Syntax : $0 number"
  echo "Use to print multiplication table for given number"
  exit 1
fi
n=$1
for i in 1 2 3 4 5 6 7 8 9 10
do
  echo "$n * $i = `expr $i \* $n`"
done

Save above script and run it as:

$ chmod 755 mtable
$ ./mtable 7
$ ./mtable

For first run, above script prints multiplication table of given number where i = 1,2 ... 10 is multiplied by given n (here command line argument 7) in order to produce multiplication table as

7 * 1 = 7
7 * 2 = 14
...
..
7 * 10 = 70

And for second test run, it will print message -

Error - Number missing form command line argument
Syntax : ./mtable number
Use to print multiplication table for given number

This happened because we have not supplied given number for which we want the multiplication table, Hence script is showing Error message, Syntax and usage of our script. This is good idea if our program takes some argument, let the user know what is the use of the script and how to use the script.

Note that to terminate our script we used 'exit 1' command which takes 1 as argument (1 indicates error and therefore script is terminated)

You can also use the following syntax:

Syntax:

         for (( expr1; expr2; expr3 ))
         do
               .....
			   ...
               repeat all statements between do and 
               done until expr2 is TRUE
         Done

In above syntax BEFORE the first iteration, expr1 is evaluated. This is usually used to initialize variables for the loop. 

All the statements between do and done is executed repeatedly UNTIL the value of expr2 is TRUE.
AFTER each iteration of the loop, expr3 is evaluated. This is usually use to increment a loop counter.

$ cat > for2
for ((  i = 0 ;  i <= 5;  i++  ))
do
  echo "Welcome $i times"
done

Run the above script as follows:

$ chmod +x for2
$ ./for2
Welcome 0 times
Welcome 1 times
Welcome 2 times
Welcome 3 times
Welcome 4 times
Welcome 5 times

In above example, first expression (i = 0), is used to set the value variableito zero.

Second expression is condition i.e. all statements between do and done executed as long as expression 2 (i.e continue as long as the value of variableiis less than or equel to 5) is TRUE.

Last expression i++ increments the value of by 1 i.e. it's equivalent to i = i + 1 statement.






Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in Linux )

Latest Articles (in Linux)