当前位置:网站首页>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}"

边栏推荐
- 思科--高可用和高可靠网络考试
- Today's sleep quality record 71 points
- Introduction and use of plantuml
- MySQL -- deduction of index storage model
- Mysql database detailed learning tutorial
- Efficiency improvement - encourage personalized container development environment
- Congratulations on the release of friends' new book (send welfare)
- 447 Bili Bili noodles warp 1
- 倒置残差的理解
- The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received
猜你喜欢

Réimpression de l'article csdn

MySQL -- index of MyISAM storage engine

今日睡眠质量记录71分

多图预警~ 华为 ECS 与 阿里云 ECS 对比实战

Deadlock handling strategies - prevent deadlock, avoid deadlock, detect and remove deadlock

Explain JMM in detail

Explain the use of locksupport in detail

Use three JS realize the 'ice cream' earth, and let the earth cool for a summer

转--深入LUA脚本语言,让你彻底明白调试原理
![Happy number [fast and slow pointer of ring PROBLEMS]](/img/37/5c94b9b062a54067a50918f94e61ea.png)
Happy number [fast and slow pointer of ring PROBLEMS]
随机推荐
人体姿态估计的热图变成坐标点的两种方案
Selection of all-optical technology in the park - Part 2
I graduated from college in 14 years and changed to software testing in 3 months. My monthly salary was 13.5k. At the age of 32, I finally found the right direction
[QT widget] encapsulates a simple thread management class
Cloud Vulnerability Global Database
Origin2018 installation tutorial "recommended collection"
[target tracking] | single target tracking indicator
“信任机器”为发展赋能
The principle, testing and Countermeasures of malicious software reverse closing EDR
Explain JMM in detail
Use and function of spark analyze command map join broadcast join
【无标题】
Vsphere+ and vsan+ are coming! VMware hybrid cloud focus: native, fast migration, mixed load
MySQL -- index of InnoDB storage engine
Pytorch's code for visualizing feature maps after training its own network
Demo program implementation of QT version Huarui camera
Kubernetes create service access pod
力扣 710. 黑名单中的随机数
Pytorch nn.functional.unfold()的简单理解与用法
元宇宙可能成为互联网发展的新方向