test命令, 当条件为真时则传回非零值,而条件为伪时则传回零。 1. if then 语法以及流程 if (condition) then then-commands fi
2. if then else if (condition) then then-commands then-commands else-commands else else-commands fi
3. if then elif if (condition1) then commands1 elif (condition2) then commands2 else commands3 fi
这里有一个例子
echo 'word \c' read word1 echo 'word \c' read word2 echo 'word \c' read word3 if ( test "$word1" = "$word2" -a "$word2" = "$word3") then echo 'Match: words 1, 2, & 3' elif (test "$word1" = "word2") then ehco 'Match: words 1 & 2' elif (test "$word1" = "word3") then echo 'Match: words 1 & 3' elif (test "$word2" = '$word3') then echo 'Match: words 2 & 3' else echo 'No match' fi
4 for in for var in arg-list do commands done
ex: for a in xx yy zz do echo $a done 5. for for var do commands done
for a do echo $a done
6. while
while (condition) do commands done
ex: number = 0 while(test $number -lt 10) do echo "$number \c" number = `expr $number + 1` done
7. until until (condition) do commands done
8. break, continue 9. case case str in part1) command1;; part2) command2;; part3) command3;; esac
echo 'Enter a, b, or c: \c' read letter case $letter_in A|a) echo 'You enter A.';; B|b) echo 'You enter B.';; C|c) echo 'You enter C.';; *) echo 'Not A, B, or C';; esac