当前位置:网站首页>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 .
边栏推荐
猜你喜欢

Exploring temporary information for dynamic network embedding

通俗易懂C语言关键字static

biggan:large scale gan training for high fidelity natural image synthesis

Abnova CMV CISH probe solution

初识Opengl

接口测试用例设计

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

-- EGFR FISH probe solution

General introduction to gun make (2)

Dataframe to list
随机推荐
如何制定可实现中长期目标?
Chinese and English instructions of collagen enzyme Worthington
图形渲染管线
Energetic girl wangyujie was invited to be the spokesperson for the global finals of the sixth season perfect children's model
高手常用的电脑快捷键
Detailed explanation of memory leak check tools
Abnova actn4 DNA probe solution
关于VS scanf出现‘scanf‘: This function or variable may be unsafe. Consider usi问题的解决方法
Redis7.0 installation steps
将lua print输出到cocos2d控制台输出窗口中
SDRAM控制器——仲裁模块的实现
PTA class a simulated ninth bullet: 1114-1117
NDK20b FFmpeg4.2.2 编译和集成
Chrome浏览器开发者工具使用
Sweet cool girl jinshuyi was invited to be the spokesperson for the global finals of the sixth season perfect children's model
Abnova CSV monoclonal antibody solution
Gun make (5) variables in makefile
recvmsg & sendmsg
xargs 的作用详解
连接投影仪