shell里面也有流程控制,其使用方法与C和Java类似,不过也存在差别
if else
if
语法格式
if condition
then
command1
command2
...
commandN
fi
if语句也可以写在一行
if [ $(ps -ef | grep -c "ssh") -gt 1 ]; then echo "true"; fi
if else
语法格式
if condition
then
command1
command2
...
commandN
else
command
fi
if else-if else
语法格式
if condition1
then
command1
elif condition2
then
command2
else
commandN
fi
for循环
语法格式
for var in item1 item2 ... itemN
do
command1
command2
...
commandN
done
也可以写成一行
for var in item1 item2 ... itemN; do command1; command2… done;
while循环
语法格式
while condition
do
command
done
无限循环
while :
do
command
done
while true
do
command
done
for (( ; ; ))
until循环
until循环执行一系列命令直至条件为true时停止
语法格式
until condition
do
command
done
case
用case语句匹配一个值与一个模式,如果匹配成功,执行相匹配的命令
语法格式
case 值 in
模式1)
command1
command2
...
commandN
;;
模式2)
command1
command2
...
commandN
;;
*)
command1
command2
...
commandN
;;
esac
跳出循环
break
break命令允许跳出所有循环
continue
continue跳出当前循环