Monday, 27 June 2011

Conditional Structures in Shell Scripting - III


After if..else  and if..elfi..else we move to looping structures. Looping is supported in the form of for and while. Again they work semantically the same but syntactically different.

for Loop
Syntax
In bash shell there are two syntaxes.

1. for variable in list
   do 
     statement1 is executed till the list exhausts
   done

2. traditional for
 for (( variable_init ; condition ; variable_increment/decrement ))
   do
              statement1 is executed 
      till the condition returns false 
      done


Example:


Same program with second syntax


while Loop
Nothing new about it. Just loops through the series of statements until the condition is true.
Syntax:
          #sensitive to white spaces
          while [ condition ]
          do
              statement1
              statement2
           done



case Statement
case in bash scripting is very similar to switch case semantically . It save the trouble for writing a multilevel elif statements.

Syntax:

case $variable in
case1)  statement1;;
case2)  statement2;;
..
..
caseN) statementN;;
esac











No comments:

Post a Comment

Popular Posts