目录
Case语句
1--case格式
2--case使用案例:输入不容的数字,给出不同的结果
跳出循环
1--break
案例:执行十次时,跳出当前循环
完整流程
2--continue
案例:跳过2,4 输出
完整流程
Case语句
1--case格式
case 变量值 in
值1)
context
;;
值2)
context
;;
值3)
context
;;
值4)
context
;;
值5)
context
;;
esac
2--case使用案例:输入不容的数字,给出不同的结果
cases.sh文件中的内容
# case 使用
echo "输入1 到 4 之间的数字"
read inputNum
case $inputNum in
1) echo "你选择了数字1"
;;
2) echo "你选择了数字2"
;;
3) echo "你选择了数字3"
;;
4) echo "你选择了数字4"
;;
*) echo "你没有选择合理范围内的数字"
;;esac
完整流程
[xijiu@localhost ~]$ vi cases.sh
[xijiu@localhost ~]$ cat cases.sh
# case 使用echo "输入1 到 4 之间的数字"
read inputNum
case $inputNum in
1) echo "你选择了数字1"
;;
2) echo "你选择了数字2"
;;
3) echo "你选择了数字3"
;;
4) echo "你选择了数字4"
;;
*) echo "你没有选择合理范围内的数字"
;;esac
[xijiu@localhost ~]$ sh cases.sh
输入1 到 4 之间的数字
3
你选择了数字3
[xijiu@localhost ~]$ sh cases.sh
输入1 到 4 之间的数字
5
你没有选择合理范围内的数字
[xijiu@localhost ~]$ sh cases.sh
输入1 到 4 之间的数字
a
你没有选择合理范围内的数字
[xijiu@localhost ~]$ sh cases.sh
输入1 到 4 之间的数字
1
你选择了数字1
[xijiu@localhost ~]$ sh cases.sh
输入1 到 4 之间的数字
4
你选择了数字4
[xijiu@localhost ~]$ sh cases.sh
输入1 到 4 之间的数字
2
你选择了数字2
[xijiu@localhost ~]$
跳出循环
1--break
break 是跳出当前整个循环
案例:执行十次时,跳出当前循环
# break 跳出当前循环
num=1
while true ; do
if [ $num -ge 10 ] ; then
break;
fi
echo $num;
(( num++ ));
done
完整流程
[xijiu@localhost ~]$ vi breakE.sh
[xijiu@localhost ~]$ cat breakE.sh
# break 跳出当前循环num=1
while true ; do
if [ $num -ge 10 ] ; then
break;
fi
echo $num;
(( num++ ));
done
[xijiu@localhost ~]$ sh breakE.sh
1
2
3
4
5
6
7
8
9
[xijiu@localhost ~]$
2--continue
continue是跳出当次循环
案例:跳过2,4 输出
# countine 循环
for i in {1..5} ; do
if [ $i -eq 2 ] ; then
continue
fiif [ $i -eq 3 ] ; then
continue
fiecho $i
done
完整流程
[xijiu@localhost ~]$ vi countines.sh
[xijiu@localhost ~]$ cat countines.sh
# countine 循环for i in {1..5} ; do
if [ $i -eq 2 ] ; then
continue
fi
if [ $i -eq 3 ] ; then
continue
fiecho $i
done
[xijiu@localhost ~]$ sh countines.sh
1
4
5
[xijiu@localhost ~]$