当前位置:网站首页>Shell learning record (III)
Shell learning record (III)
2022-06-26 02:03:00 【_ Bruce】
Shell Process control
if else-if else
Be careful : stay sh/bash You can't write that in , If else Branch has no statement execution , Don't write this else
if condition1
then
command1
elif condition2
then
command2
else
commandN
fiWrite in a line ( For terminal command prompt ):
if [ $(ps -ef | grep -c "ssh") -gt 1 ]; then echo "true"; fifor loop
for var in item1 item2 ... itemN
do
command1
command2
...
commandN
doneWrite in a line :
for var in item1 item2 ... itemN; do command1; command2… done;Output string :
for str in 'This is a string'
do
echo $str
doneOutput :
This is a stringwhile sentence
while condition
do
command
done#!/bin/bash
int=1
while(( $int<=5 )) # Be careful : Two brackets
do
echo $int
let "int++"
doneOutput :
1
2
3
4
5The above example uses Bash let command , It is used to execute one or more expressions , There is no need to add $ To represent a variable
Self adding operation :let no++
Since the reduction of operating :let no--
Abbreviation form let no+=10,let no-=20, They are equal to let no=no+10,let no=no-20
#!/bin/bash
let a=5+4
let b=9-3
echo $a $buntil loop
until Loop through a series of commands until the condition is true Stop when .
until Circulation and while The cycle is the opposite of the way it's handled .
commonly while Circulation is better than until loop , But sometimes — Only in a few cases ,until Circulation is more useful .
until Grammar format :
until condition
do
command
done#!/bin/bash
a=0
until [ ! $a -lt 10 ]
do
echo $a
a=`expr $a + 1`
doneOutput :
0
1
2
3
4
5
6
7
8
9case
Shell case Statements are multiple choice statements . It can be used case Statement matches a value with a pattern , If the match is successful , Execute the matching command .case The statement format is as follows :
case value in
Pattern 1)
command1
command2
...
commandN
;;
Pattern 2)
command1
command2
...
commandN
;;
esaccase Work as shown above . Value must be followed by word in, The first mock exam must be closed with right brackets. . Value can be variable or constant . Match found that after the first mock exam meets a certain pattern, , During this period, all commands are executed until ;;.
Value will detect every matching pattern . Once the patterns match , After the corresponding command of matching mode is executed, other modes will not be continued . If there is no matching pattern , Use the asterisk * Capture the value , Then execute the following command .
The following script prompts for 1 To 4, Match with each pattern :
echo ' Input 1 To 4 Number between :'
echo ' The number you enter is :'
read aNum
case $aNum in
1) echo ' You chose 1'
;;
2) echo ' You chose 2'
;;
3) echo ' You chose 3'
;;
4) echo ' You chose 4'
;;
*) echo ' You didn't enter 1 To 4 Number between '
;;
esacOutput :
Input 1 To 4 Number between :
The number you enter is :
3
You chose 3
Input 1 To 4 Number between :
The number you enter is :
5
You didn't enter 1 To 4 Number between Out of the loop
During the cycle , Sometimes it's necessary to force the loop out before the loop end condition is reached ,Shell Use two commands to do this :break and continue.
break command
break The command allows you to jump out of all loops ( Terminate all loops after execution ).
In the following example , The script goes into a dead loop until the number entered by the user is greater than 5. To get out of this cycle , Back to shell At the prompt , Need to use break command .
#!/bin/bash
while :
do
echo -n " Input 1 To 5 Number between :"
read aNum
case $aNum in
1|2|3|4|5) echo " The number you enter is $aNum!"
;;
*) echo " The number you entered is not 1 To 5 Between ! Game over "
break
;;
esac
doneOutput :
Input 1 To 5 Number between :3
The number you enter is 3!
Input 1 To 5 Number between :7
The number you entered is not 1 To 5 Between ! Game over continue
continue Command and break Command similar , There's only one difference , It doesn't jump out of all loops , Just jump out of the current loop .
Modify the above example :
#!/bin/bash
while :
do
echo -n " Input 1 To 5 Number between : "
read aNum
case $aNum in
1|2|3|4|5) echo " The number you enter is $aNum!"
;;
*) echo " The number you entered is not 1 To 5 Between !"
continue
echo " Game over "
;;
esac
doneRun code discovery , When the input is greater than 5 Digital time , The loop in this case does not end , sentence echo " Game over " It will never be carried out .
边栏推荐
- General introduction to gun make (2)
- NDK20b FFmpeg4.2.2 编译和集成
- 关于VS scanf出现‘scanf‘: This function or variable may be unsafe. Consider usi问题的解决方法
- Application and chemical properties of elastase
- 如何高效的完成每日的任务?
- Three factors affecting personal growth
- keda 2.7.1 scaledJob 代码简要分析
- Use of redis
- 论文阅读 Exploring Temporal Information for Dynamic Network Embedding
- Other codes,, VT,,, K
猜你喜欢

关于strlen与sizeof的区别

One minute to understand the difference between synchronous, asynchronous, blocking and non blocking

接口测试用例设计

分布式系统(二)分布式事务的理解

论文阅读 Exploring Temporal Information for Dynamic Network Embedding

shell学习记录(四)

前置++,后置++与前置--与后置--(++a,a++与--a,a--)

On the difference between strlen and sizeof

shell学习记录(一)

Sweet cool girl jinshuyi was invited to be the spokesperson for the global finals of the sixth season perfect children's model
随机推荐
It's better to finish one than start thousands of times (reprinted from Douban)
tos cos dscp 区别和作用
通俗易懂C語言關鍵字static
xargs 的作用详解
The answer skills and examples of practical cases of the second construction company are full of essence
Other codes,, VT,,, K
Tcp网络通信中各个状态的含义
轻轻松松理解指针
静态库动态库的使用
Three factors affecting personal growth
连接投影仪
CS144 环境配置
recvmsg & sendmsg
接口测试用例设计
PTA class a simulated sixth bomb: 1156-1159
Connectez Le projecteur
Pointnet/Pointnet++学习
Find the multiplication order of n
wifi 的理论速度计算方法
Sweet cool girl jinshuyi was invited to be the spokesperson for the global finals of the sixth season perfect children's model