当前位置:网站首页>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 .
边栏推荐
- 影响个人成长的三个因素
- Is the securities account recommended by qiniu safe?
- One minute to understand the difference between synchronous, asynchronous, blocking and non blocking
- Input 3 integers and output them from large to small
- 如何高效的完成每日的任务?
- Redis7.0的安装步骤
- V4L2+QT视频优化策略
- The answer skills and examples of practical cases of the second construction company are full of essence
- vtk初始化代码学习1
- VTK initialization code learning 1
猜你喜欢

Energetic girl wangyujie was invited to be the spokesperson for the global finals of the sixth season perfect children's model

Easy to understand C language keyword static

On the difference between strlen and sizeof

About vs scanf, 'scanf' appears: this function or variable may be unsafe Solutions to the problem of consumer usi

How to add a "security lock" to the mobile office of government and enterprises?

Sunshine boy chenhaotian was invited to be the spokesperson for the global finals of the sixth season perfect children's model

通俗易懂C语言关键字static

recvmsg & sendmsg

通俗易懂C語言關鍵字static

General introduction to gun make (2)
随机推荐
biggan:large scale gan training for high fidelity natural image synthesis
Pointnet/Pointnet++学习
How to set achievable medium - and long-term goals?
A solution to cross domain problems
Tengwenze, a hot-blooded boy, was invited to serve as the image ambassador of the global finals of the sixth season perfect children's model
PTA class a simulated third bomb: 1140-1143
Show spirit chenzitong was invited to be the chief experience officer of the global finals of the sixth season perfect children's model
Multi type study of Worthington collagen protease
Redis7.0的安装步骤
接口测试用例设计
keda 2.7.1 scaledJob 代码简要分析
CS144 环境配置
安装了Visual Studio 2013 Redistributable,mysql还是安装失败
Reverse output an integer
Wanglaoji pharmaceutical's public welfare activity of "caring for the most lovely people under the scorching sun" was launched in Hangzhou
buffer
-- EGFR FISH probe solution
Playful girl wangyixuan was invited to serve as the Promotion Ambassador for the global finals of the sixth season perfect children's model
shell学习记录(一)
Tcp网络通信中各个状态的含义