if [ $a == $b ] then echo"a is equal to b" else echo"a is not equal to b" fi
a=10 b=20
if [ $a == $b ] then echo"a is equal to b" elif [ $a -gt $b ] then echo"a is greater than b" elif [ $a -lt $b ] then echo"a is less than b" else echo"None of the condition met" fi
Shell Case语句
语法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
case 值 in 模式1) command1 command2 command3 ;; 模式2) command1 command2 command3 ;; *) command1 command2 command3 ;; esac
case工作方式如上所示。取值后面必须为关键字 in,每一模式必须以右括号结束。取值可以为变量或常数。匹配发现取值符合某一模式后,其间所有命令开始执行直至 ;;。;; 与其他语言中的 break 类似,意思是跳到整个 case 语句的最后。
实例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
echo'Input a number between 1 to 4' echo'Your number is:\c' read aNum case$aNumin 1) echo'You select 1' ;; 2) echo'You select 2' ;; 3) echo'You select 3' ;; 4) echo'You select 4' ;; *) echo'You do not select a number between 1 to 4' ;; esac
Shell For循环
语法:
1 2 3 4 5 6 7
for 变量 in 列表 do command1 command2 ... commandN done
列表是一组值(数字、字符串等)组成的序列,每个值通过空格分隔。每循环一次,就将列表中的下一个值赋给变量。 in 列表是可选的,如果不用它,for 循环使用命令行的位置参数。
实例:
1 2 3 4
for loop in 1 2 3 4 5 do echo"The value is: $loop" done
Shell While循环
语法
1 2 3 4
whilecommand do Statement(s) to be executed ifcommandistrue done
实例:
1 2 3 4 5 6
COUNTER=0 while [ $COUNTER -lt 5 ] do COUNTER='expr $COUNTER + 1' echo $COUNTER done
Shell until循环
语法:
1 2 3 4
untilcommand do Statement(s) to be executed untilcommandistrue done
实例:
1 2 3 4 5 6 7 8 9
#!/bin/bash
a=0
until [ ! $a -lt 10 ] do echo$a a=`expr $a + 1` done
#!/bin/bash funWithParam(){ echo"The value of the first parameter is $1 !" echo"The value of the second parameter is $2 !" echo"The value of the tenth parameter is $10 !" echo"The value of the tenth parameter is ${10} !" echo"The value of the eleventh parameter is ${11} !" echo"The amount of the parameters is $# !"# 参数个数 echo"The string of the parameters is $* !"# 传递给函数的所有参数 } funWithParam 1 2 3 4 5 6 7 8 9 34 73