当前位置:网站首页>shell 流程控制
shell 流程控制
2022-07-01 22:12:00 【响彻天堂丶】
1 if else语句
1.1 if语法
#多行
if 条件
then
命令
fi
#一行
if 条件; then 命令; fi
1.2 if else 语法
if 条件
then
命令
else
命令
fi
1.3 if elif else 语法
if 条件1
then
命令1
elif 条件2
then
命令2
elif 条件3
then
命令3
……
else
命令N
fi
1.2 demo
提示"请输入你的考试成绩:" 接收用户输入一个成绩, 之后使用if else条件句判断
要求1: 小于 60 输出"不及格"
要求2: 大于等于60 并且 小于70 输出"及格"
要求2: 大于等于70 并且 小于80 输出"中等"
要求3: 大于等于80 并且 小于90 输出"良好"
要求4: 大于等于90 并且 小于等于100 输出"优秀"
要求5: 以上不符合输出"成绩不合法"
#!/bin/bash
read -p "请您输入您的考试成绩:" score
if ((score<60))
then
echo "不及格"
elif ((score>=60 && score<70))
then
echo "及格"
elif ((score>=70 && score<80))
then
echo "中等"
elif ((score>=80 && score<90))
then
echo "良好"
elif ((score>=90 && score<=100))
then
echo "优秀"
else
echo "成绩不合法"
fi
2 内置命令:test
Shell中的 test 命令用于检查某个条件是否成立,它可以进行数值、字符和文件三个方面的测试。
2.1 语法
if test 数字1 options 数字2
then
...
fi
options具体如下:
参数 | 说明 |
---|---|
-eq | 等于则为真 |
-ne | 不等于则为真 |
-gt | 大于则为真 |
-ge | 大于等于则为真 |
-lt | 小于则为真 |
-le | 小于等于则为真 |
2.2 demo
#/bin/bash
num1=1 num2=1 num3=2
echo "num1=${num1},num2=${num2},num3=${num3}"
if test $num1 -eq $num2
then
echo "num1 和 num2 相等"
else
echo "num1 和 num2 不相等"
fi
if test $num3 -gt $num2
then
echo "num3 大于 num2"
else
echo "num3 小于 num2"
fi
3 流程控制:case语句
3.1 语法
case 值 in
匹配模式1)
命令1
命令2
...
;;
匹配模式2)
命令1
命令2
...
;;
*)
命令1
命令2
...
;;
esac
3.2 demo
#!/bin/bash
read -p "请输入一个0-7的数字:" number
case $number in
1)
echo "星期一"
;;
2)
echo "星期二"
;;
3)
echo "星期三"
;;
4)
echo "星期四"
;;
5)
echo "星期五"
;;
6)
echo "星期六"
;;
7)
echo "星期天"
;;
*)
echo "您输入的数字无效"
;;
esac
4 流程控制:while语句
4.1 语法
while 条件
do
命令1
命令2
...
continue; # 结束当前这一次循环, 进入下一次循环
break; # 结束当前循环
done
4.2 demo
#!/bin/bash
read -p "请您输入一个数字:" number
let i=1
while (( $i <= $number))
do
echo "第${i}次打印: hello world"
((i++))
done
5 流程控制:until语句
5.1 语法
until 条件
do
命令
done
5.2 demo
#!/bin/bash
read -p "请您输入一个数字:" number
let i=1
until (( $i >= $number ))
do
echo "第${i}次打印:hello world"
((i++))
done
6 流程控制:for语句
6.1 语法
for var in item1 item2 ... itemN
do
命令1
命令2
...
done
#start: 循环范围的起始值,必须为整数
#end: 循环范围的结束值, 必须为整数
for var in {
start..end}
do
命令
done
for((i=start;i<=end;i++))
do
命令
done
6.2 demo
#!/bin/bash
echo "##########循环方式1############"
for i in 1 2 3 4 5
do
echo "第 ${i}次打印: hello world"
done
echo "##########循环方式2############"
for i in {
1..5}
do
echo "第 ${i}次打印: hello world"
done
echo "##########循环方式3############"
for((i=1;i<=5;i++))
do
echo "第 ${i}次打印: hello world"
done
6.3 无线循环
for((;;)); do 命令; done
#!/bin/bash
let i=1
for((;;))
do
echo "第${i}次打印: hello world"
if test $i -eq 10
then
break
fi
((i++))
done
7 流程控制:select语句
select in 循环用来增强交互性,它可以显示出带编号的菜单,用户输入不同的编号就可以选择不同的菜单,并执行不同的功能. select in 是 Shell 独有的一种循环,非常适合终端(Terminal)这样的交互场景, 其他语言没有;
7.1 语法
select var in menu1 menu2 ...
do
命令
done
7.2 demo
#!/bin/bash
echo "您的爱好是什么?"
select hobby in "编程" "游戏" "篮球" "游泳"
do
echo "您选择的爱好是:${hobby}"
break
done
echo "您的爱好是: ${hobby}"
边栏推荐
猜你喜欢
[MySQL] index classification
【无标题】
[JUC learning road day 9] barrier derivatives
使用3DMax制作一个象棋棋子
Turn -- go deep into Lua scripting language, so that you can thoroughly understand the debugging principle
leetcode - 287. Find duplicates
tcpdump命令使用详解
Understanding of indexes in MySQL
Using emqx cloud to realize one machine one secret verification of IOT devices
map容器
随机推荐
Explain the volatile keyword
正则系列之组和范围(Groups and Ranges)
Appium automated testing foundation - Supplement: introduction to desired capabilities parameters
Digital currency: far-reaching innovation
Cloud Vulnerability Global Database
Favorite transaction code management tool in SAP GUI
思科考试--路由的概念和配置考试
ESP自动下载电路设计
使用3DMax制作一个象棋棋子
ECMAScript 2022 was officially released. Have you heard about it?
104. SAP UI5 表格控件的支持复选(Multi-Select)以及如何用代码一次选中多个表格行项目
Vsphere+ and vsan+ are coming! VMware hybrid cloud focus: native, fast migration, mixed load
[image segmentation] 2021 segformer neurips
Talk about what parameters ZABBIX monitors
Share some feelings of a programmer who has experienced layoffs twice a year
阿洛迷茫后的思考
Map container
Understanding of inverted residuals
下班前几分钟,我弄清了v-model与.sync的区别
El input text field word limit, beyond which the display turns red and input is prohibited