for loop in Linux Shell Script

By: Dorris Emailed: 1644 times Printed: 2119 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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

Before try to understand above syntax try the following script:

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

Run it 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 print multiplication table of given number where i = 1,2 ... 10 is multiply 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 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 use of the script and how to used 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)

Even you can use 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 variable i to 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 variable i is less than or equel to 5) is TRUE.
Last expression i++ increments the value of i by 1 i.e. it's equivalent to i = i + 1 statement.


Linux Home | All Linux Tutorials | Latest Linux Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(1)


1. View Comment

#!/bin/bash

echo "Enter number for table :"
read x


for i in 1 2 3 4 5 6 7 8 9 10
do
c=$(( $x * $i ))
echo "$x * $i = $c"
done


View Tutorial          By: ashish at 2011-03-25 07:46:09

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Dorris
sum of all digits in a given number using Shell Script
sh -x and sh -v in Linux
case..esac..in Linux Shell Script
while loop in Linux Shell Script
Nested for loop in Linux Shell Script
for loop in Linux Shell Script
Multilevel if-then-else in Linux Shell Script
Nested if-else-fi in Linux Shell Script
if...else...fi in Linux Shell Script
test command or [ expr ] in Linux Shell Script
if condition in Linux Shell Script
Download Linux. Which linux and where?
Formula in Hibernate
Primary keys assigned by triggers in Hibernate
Assigned identifiers in Hibernate

More Tutorials in Linux
smskannel SMS gateway run in background
Running jar files in background in ssh window
Could not open '': No such file or directory at lib/ExtUtils/MM_Unix.pm line 2697
Can't locate ExtUtils/MakeMaker.pm in @INC ...
bash: make: command not found
apxs: command not found
bash: svn: command not found
make: Nothing to be done for `all'.
How to burn your CD / DVD ISO image using Media Creator (Adaptec/Roxio) on Windows
How to burn your CD / DVD ISO image using Nero Burning ROM (Ahead Software) on Windows
How to burn your CD / DVD ISO image using Nero Express (Ahead Software) on Windows
How to burn your CD / DVD ISO image using NISO Recorder V2 Power Toy on Windows
How to burn your CD / DVD ISO image using k3b on CentOS
Compiling and Installing software from source in Linux
Installing RPM's using the Redhat Package Manager in Linux

More Latest News
Most Viewed Articles (in Linux )
Nested if-else-fi in Linux Shell Script
Nested for loop in Linux Shell Script
for loop in Linux Shell Script
sum of all digits in a given number using Shell Script
Calculator tutorial in Shell Script
test command or [ expr ] in Linux Shell Script
Check for leap year in Shell Script
Command line arguments in Shell Script
Find the biggest number in Shell Script
Digital Clock in Shell Script
Menu based programs in Shell Script
Compiling and Installing software from source in Linux
bash: svn: command not found
Variables in Linux Shell Script
How to define User defined variables in Linux Shell Script
Most Emailed Articles (in Linux)
bash: make: command not found
smskannel SMS gateway run in background
Input - Output redirection in Linux Shell Script
sum of all digits in a given number using Shell Script
How to burn your CD / DVD ISO image using NISO Recorder V2 Power Toy on Windows
How to use Linux?
getopts tutorial in Shell Script
How to burn your CD / DVD ISO image using Nero Express (Ahead Software) on Windows
How to burn your CD / DVD ISO image using k3b on CentOS
bash: svn: command not found
Could not open '': No such file or directory at lib/ExtUtils/MM_Unix.pm line 2697
Mounting cdrom in cent os 5 linux
Download Linux. Which linux and where?
case..esac..in Linux Shell Script
Print contents of a file using Shell Script