Shell 编程:流程控制
if 语句
if condition
then
command1
command2
...
commandN
fi
if condition
then
command1
command2
...
commandN
else
command
fi
if condition1
then
command1
elif condition2
then
command2
else
commandN
fi
条件语句
- test 命令
[...]
等价于 test 命令
((...))
用于整数运算
[[...]]
Bash 提供的扩展关键字
for 循环
for var in item1 item2 ... itemN
do
command1
command2
...
commandN
done
for 无限循环
for (( ; ; ))
while 循环
while condition
do
command
done
while 无限循环
while :
do
command
done
while true
do
command
done
until 循环
until condition
do
command
done
break 跳出循环
#!/bin/bash
while :
do
echo -n "输入 1 到 5 之间的数字:"
read aNum
case $aNum in
1|2|3|4|5) echo "你输入的数字为 $aNum!"
;;
*) echo "你输入的数字不是 1 到 5 之间的! 游戏结束"
break
;;
esac
done
continue 跳过本次循环
#!/bin/bash
while :
do
echo -n "输入 1 到 5 之间的数字: "
read aNum
case $aNum in
1|2|3|4|5) echo "你输入的数字为 $aNum!"
;;
*) echo "你输入的数字不是 1 到 5 之间的!"
continue
echo "游戏结束"
;;
esac
done