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) doneTry the following script: http://siber.cankaya.edu.tr/SystemsProgramming/cfiles/testfortestfor
for i in 1 2 3 4 5 do echo "Welcome $i times" doneAlso try the following script: http://siber.cankaya.edu.tr/SystemsProgramming/cfiles/mtablemtable
#!/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`" doneSyntax:
for (( expr1; expr2; expr3 )) do repeat all statements between do and done until expr2 is TRUE Donehttp://siber.cankaya.edu.tr/SystemsProgramming/cfiles/testfor2testfor2
#!/bin/sh # #Script to test for loop 2 # # for (( i = 0 ; i <= 5; i++ )) do echo "Welcome $i times" done
#!/bin/sh # # Nesting of for loop # # for (( i = 1; i <= 5; i++ )) ### Outer for loop ### do for (( j = 1 ; j <= 5; j++ )) ### Inner for loop ### do echo -n "$i " done echo "" #### print the new line ### doneHere, for each value of i the inner loop is cycled through 5 times, with the variable j taking values from 1 to 5. The inner for loop terminates when the value of j exceeds 5, and the outer loop terminates when the value of i exceeds 5.
#!/bin/sh # # Chessboard # # for (( i = 1; i <= 9; i++ )) ### Outer for loop ### do for (( j = 1 ; j <= 9; j++ )) ### Inner for loop ### do tot=`expr $i + $j` tmp=`expr $tot % 2` if [ $tmp -eq 0 ]; then echo -e -n "\033[47m " else echo -e -n "\033[40m " fi done echo -e -n "\033[40m " #### set back background color to black echo "" #### print the new line ### done echo -e -n "\033[47m " #### set back background color to white echo "" #### print the new line ### echo "" #### print the new line ### echo "" #### print the new line ### echo "" #### print the new line ###
while [ condition ] do command1 command2 command3 .. .. done
#!/bin/sh # #Script to test while statement # # 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 i=1 while [ $i -le 10 ] do echo "$n * $i = `expr $i \* $n`" i=`expr $i + 1` done
case $variable-name in pattern1) command .. command;; pattern2) command .. command;; patternN) command .. command;; *) command .. command;; esac
#!/bin/sh # # if no vehicle name is given # i.e. -z $1 is defined and it is NULL # # if no command line arg if [ -z $1 ] then rental="*** Unknown vehicle ***" elif [ -n $1 ] then # otherwise make first arg as rental rental=$1 fi case $rental in "car") echo "For $rental Rs.20 per k/m";; "van") echo "For $rental Rs.10 per k/m";; "jeep") echo "For $rental Rs.5 per k/m";; "bicycle") echo "For $rental 20 paisa per k/m";; *) echo "Sorry, I can not get a $rental for you";; esac