May 22, 2021 Linux
In different languages such as Java and PHP, sh's process control cannot be empty, e.g. (the following is PHP process control writing):
<?php if (isset($_GET["q"])) {
search(q);
}
else {
// 不做任何事情
}
You can't write that in sh/bash, and if the else branch doesn't have a statement to execute, don't write this else, like this
If statement syntax format:
if condition
then
command1
command2
...
commandN
fi
Write as a line (for terminal command prompts):
if [ $(ps -ef | grep -c "ssh") -gt 1 ]; then echo "true"; fi
The fi at the end is if spelling upside down, and you'll see something similar later.
if else syntax format:
if condition
then
command1
command2
...
commandN
else
command
fi
if else-if else syntax format:
if condition1
then
command1
elif condition2
then
command2
else
commandN
fi
If else statements are often used in conjunction with test commands, as follows:
num1=$[2*3]
num2=$[1+5]
if test $[num1] -eq $[num2]
then
echo '两个数字相等!'
else
echo '两个数字不相等!'
fi
Output:
两个数字相等!
Like other programming languages, Shell supports for loops.
The for loop is generally in the following format:
for var in item1 item2 ... itemN
do
command1
command2
...
commandN
done
Write it in a line:
for var in item1 item2 ... itemN; do command1; command2… done;
When the variable value is in the list, the for loop executes all commands at once, using the variable name to get the current value in the list. C ommands can be any valid shell commands and statements. The in list can contain replacements, strings, and file names.
The in list is optional, and if you don't use it, for loops the position parameters of the command line.
For example, the numbers in the current list are output in order:
for loop in 1 2 3 4 5
do
echo "The value is: $loop"
done
Output:
The value is: 1
The value is: 2
The value is: 3
The value is: 4
The value is: 5
Characters in the sequential output string:
for str in 'This is a string'
do
echo $str
done
Output:
This is a string
While loops are used to continuously execute a series of commands, also used to read data from an input file; The format is:
while condition
do
command
done
The following is a basic while loop, tested with the condition that if the int is less than equal to 5, then the condition returns true. I nt starts at 0, and each time the loop is processed, int plus 1. Run the script above, return the numbers 1 through 5, and then terminate.
#!/bin/sh
int=1
while(( $int<=5 )) do echo $int let "int++" done
Run the script and output:
1
2
3
4
5
The while loop can be used to read keyboard information. In the following example, the input information is set to the variable FILM, and the loop is ended by pressing .lt;Ctrl-D.gt.
echo '按下 <CTRL-D> 退出'
echo -n '输入你最喜欢的电影名: '
while read FILM
do
echo "是的!$FILM 是一部好电影"
done
Run the script and output something like this:
按下 <CTRL-D> 退出
输入你最喜欢的电影名: W3Cschool在线教程
是的!W3Cschool在线教程 是一部好电影
Infinite loop syntax format:
while :
do
command
done
Or
while true
do
command
done
Or
for (( ; ; ))
The until loop executes a series of commands until the condition is true.
The until loop is the exact opposite of the while loop.
Generally while loops are better thanuntil loops, but at some point - and only rarely - the until loop is more useful.
Until syntax format:
until condition
do
command
done
The condition can be any test condition, and the test occurs at the end of the loop, so the loop is executed at least once -- be aware of this.
The Shell case statement is a multi-select statement. Y ou can match a value with a pattern with a case statement and, if successful, execute a matching command. The case statement is in the following format:
case 值 in
模式1)
command1
command2
...
commandN
;;
模式2)
command1
command2
...
commandN
;;
esac
Case works as shown above. T he value must be followed by the word in, and each pattern must end in closing brackets. T he value can be a variable or a constant. When the match finds that the value conforms to a pattern, all commands begin to execute until ; ;
The value is used to detect each pattern that matches. O nce the patterns match, no other patterns continue after the corresponding command for the matching pattern is executed. If there is no matching mode, capture the value with an asterisk, and then execute the subsequent commands.
The following script prompts you to enter 1 to 4 to match each pattern:
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
Entering different content will result in different results, such as:
输入 1 到 4 之间的数字:
你输入的数字为:
3
你选择了 3
During a loop, you sometimes need to force a jump out of a loop when the end-of-loop condition is not reached, and shell uses two commands to do so: break and continue.
The break command allows all loops to jump out (ending all loops that follow execution).
In the following example, the script goes into a dead loop until the user enters a number greater than 5. To jump out of this loop and return to the shell prompt, you need to use the break command.
#!/bin/bash
while :
do
echo -n "输入 1 到 5 之间的数字:"
read aNum
case $aNum in
1|2|3|4|5) echo "你输入的数字为 $aNum!"
;;
*) echo "你输入的数字不是 1 到 5 之间的! 游戏结束"
break
;;
esac
done
The above code is executed and the output is:
输入 1 到 5 之间的数字:3
你输入的数字为 3!
输入 1 到 5 之间的数字:7
你输入的数字不是 1 到 5 之间的! 游戏结束
The continue command is similar to the break command, with only one difference, it does not jump out of all loops, just out of the current loop.
Make changes to the example above:
#!/bin/bash
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
Running code finds that when you enter a number greater than 5, the loop in this example does not end and the statement echo "Game is over!" is never executed.
Case's syntax is very different from the Family language, which requires an esac (that is, case in turn) as an end tag, with each case branch represented by a closing parenthesis and two cents.