当前位置:网站首页>Shell process control
Shell process control
2022-07-01 23:51:00 【Resound through heaven】
1 if else sentence
1.1 if grammar
# Multiple lines
if Conditions
then
command
fi
# a line
if Conditions ; then command ; fi
1.2 if else grammar
if Conditions
then
command
else
command
fi
1.3 if elif else grammar
if Conditions 1
then
command 1
elif Conditions 2
then
command 2
elif Conditions 3
then
command 3
……
else
command N
fi
1.2 demo
Tips " Please enter your test scores :" Receive users to enter a score , Then use if else Conditional sentence judgement
requirement 1: Less than 60 Output " fail, "
requirement 2: Greater than or equal to 60 also Less than 70 Output " pass "
requirement 2: Greater than or equal to 70 also Less than 80 Output " secondary "
requirement 3: Greater than or equal to 80 also Less than 90 Output " good "
requirement 4: Greater than or equal to 90 also Less than or equal to 100 Output " good "
requirement 5: The above does not conform to the output " The results are illegal "
#!/bin/bash
read -p " Please enter your examination result :" score
if ((score<60))
then
echo " fail, "
elif ((score>=60 && score<70))
then
echo " pass "
elif ((score>=70 && score<80))
then
echo " secondary "
elif ((score>=80 && score<90))
then
echo " good "
elif ((score>=90 && score<=100))
then
echo " good "
else
echo " The results are illegal "
fi

2 Built in commands :test
Shell Medium test The command is used to check if a condition holds , It can do numerical 、 Character and file testing .
2.1 grammar
if test Numbers 1 options Numbers 2
then
...
fi
options As follows :
| Parameters | explain |
|---|---|
| -eq | Equal is true |
| -ne | True if not equal |
| -gt | Greater than is true |
| -ge | True if greater than or equal to |
| -lt | Less than is true |
| -le | True if less than or equal to |
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 and num2 equal "
else
echo "num1 and num2 It's not equal "
fi
if test $num3 -gt $num2
then
echo "num3 Greater than num2"
else
echo "num3 Less than num2"
fi

3 Process control :case sentence
3.1 grammar
case value in
Matching mode 1)
command 1
command 2
...
;;
Matching mode 2)
command 1
command 2
...
;;
*)
command 1
command 2
...
;;
esac
3.2 demo
#!/bin/bash
read -p " Please enter a 0-7 The number of :" number
case $number in
1)
echo " Monday "
;;
2)
echo " Tuesday "
;;
3)
echo " Wednesday "
;;
4)
echo " Thursday "
;;
5)
echo " Friday "
;;
6)
echo " Saturday "
;;
7)
echo " Sunday "
;;
*)
echo " The number you entered is invalid "
;;
esac

4 Process control :while sentence
4.1 grammar
while Conditions
do
command 1
command 2
...
continue; # End the current cycle , Enter next cycle
break; # End the current cycle
done
4.2 demo
#!/bin/bash
read -p " Please enter a number :" number
let i=1
while (( $i <= $number))
do
echo " The first ${i} Second print : hello world"
((i++))
done

5 Process control :until sentence
5.1 grammar
until Conditions
do
command
done
5.2 demo
#!/bin/bash
read -p " Please enter a number :" number
let i=1
until (( $i >= $number ))
do
echo " The first ${i} Second print :hello world"
((i++))
done

6 Process control :for sentence
6.1 grammar
for var in item1 item2 ... itemN
do
command 1
command 2
...
done
#start: The starting value of the cycle range , Must be an integer
#end: The end value of the loop range , Must be an integer
for var in {
start..end}
do
command
done
for((i=start;i<=end;i++))
do
command
done
6.2 demo
#!/bin/bash
echo "########## Circulation mode 1############"
for i in 1 2 3 4 5
do
echo " The first ${i} Second print : hello world"
done
echo "########## Circulation mode 2############"
for i in {
1..5}
do
echo " The first ${i} Second print : hello world"
done
echo "########## Circulation mode 3############"
for((i=1;i<=5;i++))
do
echo " The first ${i} Second print : hello world"
done
6.3 Wireless loop
for((;;)); do command ; done
#!/bin/bash
let i=1
for((;;))
do
echo " The first ${i} Second print : hello world"
if test $i -eq 10
then
break
fi
((i++))
done

7 Process control :select sentence
select in Loops are used to enhance interactivity , It can display numbered menus , Users can choose different menus by entering different numbers , And perform different functions . select in yes Shell A unique cycle , Very suitable for terminals (Terminal) Such an Interaction scenario , No other language ;
7.1 grammar
select var in menu1 menu2 ...
do
command
done
7.2 demo
#!/bin/bash
echo " What is your hobby ?"
select hobby in " Programming " " game " " Basketball " " swimming "
do
echo " Your chosen hobby is :${hobby}"
break
done
echo " Your hobby is : ${hobby}"

边栏推荐
- .env.xxx 文件,加了常量,卻undefined
- Use pair to do unordered_ Key value of map
- 安全协议重点
- The third part of the construction of the defense system of offensive and defensive exercises is the establishment of a practical security system
- SQL optimization
- 2021 RoboCom 世界机器人开发者大赛-高职组复赛
- How to display real-time 2D map after rviz is opened
- [Qt] résoudre le problème que Qt msvc 2017 ne peut pas Compiler
- Key points and difficulties of the course "information content security" at Harbin Institute of Technology
- const // It is a const object...class nullptr_t
猜你喜欢

PyCharm调用matplotlib绘图时图像弹出问题怎么解决

第六章 数据流建模

How to display real-time 2D map after rviz is opened

S32Kxxx bootloader之UDS bootloader

Future trend and development of neural network Internet of things

BlocProvider为什么感觉和Provider很相似?

【QT】QtCreator卸载与安装(非正常状态)

notBlank 和 notEmpty

Redis 主从同步
![[must] bm41 output the right view of the binary tree [medium +]](/img/a5/00b2f0df5ab448665a2b062d145e52.png)
[must] bm41 output the right view of the binary tree [medium +]
随机推荐
Know --matplotlib
【必会】BM41 输出二叉树的右视图【中等+】
[leetcode] length of the last word [58]
正则表达式收集
华为HMS Core携手超图为三维GIS注入新动能
在代码中使用SqlCommand对象
cookie、session、tooken
UDS bootloader of s32kxxx bootloader
[swoole Series 1] what will you learn in the world of swoole?
Oracle中已定义者身份执行函数AUTHID DEFINER与Postgresql行为的异同
Li Kou today's question -241 Design priorities for operational expressions
2022-07-01: at the annual meeting of a company, everyone is going to play a game of giving bonuses. There are a total of N employees. Each employee has construction points and trouble points. They nee
Multi table operation - one to one, one to many and many to many
北京炒股开户选择手机办理安全吗?
股票开户哪个证券公司最好,有安全保障吗
Regular expression collection
Postgresql随手记(10)动态执行EXECUTING语法解析过程
【无标题】
有没有一段代码,让你为人类的智慧所折服
How to display real-time 2D map after rviz is opened