Shell Scripting Tutorial [8] - Shell Process Control
Directory : https://blog.csdn.net/shn111/article/details/131590488
Reference tutorial : https://www.runoob.com/linux/linux-shell.html
Online editor : https://www.runoob.com/try/runcode.php?filename=helloworld&type=bash
The flow control of the shell cannot be empty
The following writing is wrong, if there is no statement execution in the else branch, do not write this else
if [ true ]
then
echo "hello"
else
fi
if else
if
Statement Syntax Format
if condition
then
command1
command2
...
commandN
fi
write in one line (for terminal command prompt)
if [ $(ps -ef | grep -c "ssh") -gt 1 ]; then echo "true"; fi
if else
grammatical format
if condition
then
command1
command2
...
commandN
else
command
fi
if else-if else
grammatical format
if condition1
then
command1
elif condition2
then
command2
else
commandN
fi
The […] judgment statement of if else is greater than using -gt, less than using -lt
if [ "$a" -gt "$b" ]; then
...
fi
If you use ((…)) as a judgment statement, you can directly use > and < for greater than and less than.
if (( a > b )); then
...
fi
Example:
a=10
b=20
if [ $a == $b ]
then
echo "a 等于 b"
elif [ $a -gt $b ]
then
echo "a 大于 b"
elif [ $a -lt $b ]
then
echo "a 小于 b"
else
echo "没有符合的条件"
fi
# a 小于 b
Use ((…)) as a judgment statement:
a=10
b=20
if (( $a == $b ))
then
echo "a 等于 b"
elif (( $a > $b ))
then
echo "a 大于 b"
elif (( $a < $b ))
then
echo "a 小于 b"
else
echo "没有符合的条件"
fi
# a 小于 b
The if else statement is often used in conjunction with the test command, as follows:
num1=$[2*3]
num2=$[1+5]
if test $[num1] -eq $[num2]
then
echo '两个数字相等!'
else
echo '两个数字不相等!'
fi
# 两个数字相等!
for loop
Format:
for var in item1 item2 ... itemN
do
command1
command2
...
commandN
done
Written in one line:
for var in item1 item2 ... itemN; do command1; command2… done;
When the variable value is in the list, the for loop executes all the commands once, and uses the variable name to get the current value in the list. command can be any valid shell command and statement. The in list can contain substitutions, strings, and filenames.
The in list is optional, and if you don't use it, the for loop uses positional arguments from the command line.
For example, to print the numbers in the current list sequentially:
for loop in 1 2 3 4 5
do
echo "The value is: $loop"
done
# The value is: 1
# The value is: 2
# The value is: 3
# The value is: 4
# The value is: 5
Print the characters in a string sequentially:
for str in This is a string
do
echo $str
done
# This
# is
# a
# string
while loop
A while loop is used to continuously execute a sequence of commands, and is also used to read data from an input file. Its syntax format is:
while condition
do
command
done
example
int=1
while(( $int<=5 ))
do
echo $int
let "int++"
done
# 1
# 2
# 3
# 4
# 5
The above example uses the Bash let command, which is used to execute one or more expressions, and there is no need to add $ to represent variables in variable calculations. For details, please refer to: Bash let command
The while loop can be used to read keyboard information. In the example below, the input information is set to the variable FILM, press to end the loop.
echo '按下 <CTRL-D> 退出'
echo -n '输入你最喜欢的网站名: '
while read FILM
do
echo "是的!$FILM 是一个好网站"
done
Infinite loop
Grammar format:
while :
do
command
done
or
while true
do
command
done
or
for (( ; ; ))
until loop
until Loop executes a sequence of commands until the condition is true
true.
The until loop is the exact opposite of the while loop.
In general a while loop is better than an until loop, but sometimes—and only rarely—an until loop is more useful.
Grammar format:
until condition
do
command
done
condition is generally a conditional expression, if the return value is false, continue to execute the statement in the loop body, otherwise jump out of the loop.
Example:
a=0
until [ ! $a -lt 10 ]
do
echo $a
a=`expr $a + 1`
done
# 0
# 1
# 2
# 3
# 4
# 5
# 6
# 7
# 8
# 9
case…esac
case ... esac is a multi-choice statement, which is similar to switch ... case statements in other languages. It is a multi-branch selection structure. Each case branch starts with a right parenthesis, and two semicolons ;; indicate break, which means the end of execution , jump out of the entire case ... esac statement, and esac (that is, the reverse of case) is used as the end tag.
You can use a case statement to match a value and a pattern, and if the match is successful, execute the matching command.
grammatical format
case 值 in
模式1)
command1
command2
...
commandN
;;
模式2)
command1
command2
...
commandN
;;
esac
The working method of case is as shown above, the value must be followed by the word in, and each pattern must end with a closing bracket. The value can be a variable or a constant. After the matching finds that the value matches a certain pattern, all commands will be executed until ;;.
A value will detect every pattern that matches. Once the pattern is matched, no other patterns will be continued after the corresponding command matching the pattern is executed. If none of the patterns match, use an asterisk * to capture the value before executing the following command.
Example:
echo '输入 1 到 4 之间的数字:'
echo '你输入的数字为:'
read aNum
case $aNum in
1) echo '你选择了 1'
;;
2) echo '你选择了 2'
;;
3) echo '你选择了 3'
;;
4) echo '你选择了 4'
;;
*) echo '你没有输入 1 到 4 之间的数字'
;;
esac
Match string:
site="runoob"
case "$site" in
"runoob") echo "菜鸟教程"
;;
"google") echo "Google 搜索"
;;
"taobao") echo "淘宝网"
;;
esac
# 菜鸟教程
break out of the loop
break
Command
The break command allows breaking out of all loops (terminating the execution of all subsequent loops).
Example: The script enters an infinite loop until the user enters a number greater than 5. To break out of this loop and return to the shell prompt, you need to use the break command.
while :
do
echo -n "输入 1 到 5 之间的数字:"
read aNum
case $aNum in
1|2|3|4|5) echo "你输入的数字为 $aNum!"
;;
*) echo "你输入的数字不是 1 到 5 之间的! 游戏结束"
break
;;
esac
done
# 输入 1 到 5 之间的数字:3
# 你输入的数字为 3!
# 输入 1 到 5 之间的数字:7
# 你输入的数字不是 1 到 5 之间的! 游戏结束
continue
Command
The continue command is similar to the break command, the only difference is that it does not break out of all loops, only the current loop.
Example: When a number greater than 5 is entered, the loop in this example will not end, and the statement echo "game over" will never be executed.
while :
do
echo -n "输入 1 到 5 之间的数字: "
read aNum
case $aNum in
1|2|3|4|5) echo "你输入的数字为 $aNum!"
;;
*) echo "你输入的数字不是 1 到 5 之间的!"
continue
echo "游戏结束"
;;
esac
done