if else
if
if condition
then
command1
command2
...
commandN
fi
或
if [ condition ]; then command; fi
if-else
if condition
then
command1
command2
...
commandN
else
command
fi
if else-if else
if condition1
then
command1
else if condition2
then
command2
else
commandN
fi
for 循环
for var in item1 item2 ... itemN
do
commanf1
command2
...
commandN
done
或
for var in item1 item2 ... itemN;do command1;command2;...commandN;done
while 语句
while condition
do
command
done
无限循环
while true
do
command
done
或
while :
do
command
done
或
for (( ; ; ))
until 循环
condition一般为条件大表达,如果返回值为false,则继续执行循环体内的语句,否则跳出循环。
a=0
until [ ! $a -lt 10]
do
echo $a
a=`expr $a + 1`
done
case
- 取值后面必须为单词in
- 每一模式必须以有括号结束
- 取值可以为变量或常数
- 如果无匹配模式,使用*捕获该值
case 值 in
模式1)
command1
command2
...
commandN
;;
模式2)
command1
command2
...
commandN
;;
esac
跳出循环
break
continue
esac
esac是case反过来,作为结束标记,每个case分支用右圆括号,用两个分号表示break.