当前位置:网站首页>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}"
边栏推荐
- Understanding of inverted residuals
- leetcode - 287. Find duplicates
- Daily question brushing record (10)
- General use of qstringlist
- Explain kubernetes network model in detail
- pytorch训练自己网络后可视化特征图谱的代码
- tcpdump命令使用详解
- The fixed assets management subsystem reports are divided into what categories and which accounts are included
- QStringList 的常规使用
- 【无标题】
猜你喜欢
"Trust machine" empowers development
Introduction and use of plantuml
Turn -- the underlying debugging principle of GDB is so simple
转--原来gdb的底层调试原理这么简单
Demo program implementation of QT version Huarui camera
Cut noodles C language
Hide the creation and use of users
正则系列之量词(Quantifiers)
思科考试--路由的概念和配置考试
Understanding of indexes in MySQL
随机推荐
mixconv代码
Yolov5.5 call local camera
Pytorch nn.functional.unfold()的简单理解与用法
Little red book scheme jumps to the specified page
[MySQL] index classification
The median salary of TSMC's global employees is about 460000, and the CEO is about 8.99 million; Apple raised the price of iPhone in Japan; VIM 9.0 release | geek headlines
Fiori 应用通过 Adaptation Project 的增强方式分享
Cisco -- highly available and reliable network examination
Happy number [fast and slow pointer of ring PROBLEMS]
Flink SQL command line connection yarn
Fiori applications are shared through the enhancement of adaptation project
转--原来gdb的底层调试原理这么简单
Deep learning -- data operation
Share some feelings of a programmer who has experienced layoffs twice a year
MySQL -- index of InnoDB storage engine
Chen Tianqi's machine learning compilation course (free)
Stimulate new kinetic energy and promote digital economy in multiple places
SAP 智能机器人流程自动化(iRPA)解决方案分享
思科考试--冗余网络
Explain the use of locksupport in detail