Shell 流程控制
if else
格式
if condition1
then
command1
elif condition2
then
command2
else
commandN
fi
实例
num1=100
num2=100
if test $num1 -eq $num2
then
echo "=="
else
echo "!="
fi
for 循环
格式和实例
for var in item1 item2 ... itemN
do
command1
command2
...
commandN
done
或者
for((i;condition;next_i));do
command_1;
command_2;
commond_..;
done;
for val in 1 2 3 4 5
do
echo $val
done
while 语句 和 case
格式
#While
while condition
do
command
done
#Case
case 值 in
模式1)
command1
...
commandN
;;
模式2)
command1
...
commandN
;;
esac
实例
while true
do
echo "input num 1 - 4"
read num
case $num in
1|2|3|4) echo "YES"
;;
5|6|7|8|9) echo "BIG"
continue
;;
*) echo "NO"
break
;;
esac
done
无限循环写法
while true
do
command
done
或者
for (( ; ; ))