Thursday 9 January 2014

SHELL Flow control structure




There are a few common instruction flow control structure in BASH including if, case, while, for, until. This blog will give a brief introduction. 

IF: to execute the commands according to the expression value.

The basic structure is
If [expression] ; then

[commands]

else

[commands]

fi

example:



CASE: multiple value evaluation structure.

case [expression] in

       mode1 [| mode2] ) commands;;

       mode3 [| mode4] ) commands;;

       mode5 [| mode6] ) commands;;

       *) commands;;

esac

for example:



Select: generate a menu for use to select from. Usually combined with CASE

Example:



While : loop the commands while the expression value is true

While [expression] ; do

[commands]
Done





For loop: iterate the variable in the parameter list


format:
for variable in list
do

commands

done
example:












break and continue are used to change the normal loop/while flow:


  • break: exit the loop/while structure
  • continue: ignore the left commands but start the next round of loop/while again.




No comments:

Post a Comment