Shell Flow Control
The flow control of shell cannot be empty which is different from other languages such as Java and PHP.The following is the writing of PHP flow control:
<?php
if (isset($_GET["q"])) {
search(q);
}
else {
// do nothing
}
We cannot do like this in sh / bash. If there is no statement execution in the else branch, don’t write ‘else’.
if else
if
The if statement has the following syntax:
if condition
then
command1
command2
...
commandN
fi
Write in a line (applicable to terminal command prompt):
if [ $(ps -ef | grep -c "ssh") -gt 1 ]; then echo "true"; fi
The fi at the end is if spelled reverse, we will encounter similar situation later.
if else
The if else statement has the following syntax:
if condition
then
command1
command2
...
commandN
else
command
fi
if else-if else
The if else-if else statement has the following syntax:
if condition1
then
command1
elif condition2
then
command2
else
commandN
fi
The following example determines whether two variables are equal:
a=10
b=20
if [ $a == $b ]
then
echo "a is equal to b"
elif [ $a -gt $b ]
then
echo "a is greater than b"
elif [ $a -lt $b ]
then
echo "a is less than b"
else
echo "no conditions meet"
fi
Output is
a is less than 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 'The two numbers are equal!'
else
echo 'The two numbers are not equal!'
fi
Output is
The two numbers are not equal!
The for loop
Shell supports for loops which is similar to other programming languages.
The general format of the for loop is:
for var in item1 item2 ... itemN
do
command1
command2
...
commandN
done
Write in a line:
for var in item1 item2 ... itemN; do command1; command2… done;
The command can be any valid shell commands and statements.The in list can contain substitutions, strings, and file names.
The in is optional.
For example, output the numbers in the current list sequentially:
for loop in 1 2 3 4 5
do
echo "The value is: $loop"
done
Output is
The value is: 1 The value is: 2 The value is: 3 The value is: 4 The value is: 5
For example, output the characters in the string sequentially:
for str in 'This is a string'
do
echo $str
done
Output is
This is a string
The while loop
The while loop enables you to execute a set of commands repeatedly, or read data from input files.The syntax is
while condition
do
command
done
The following is a basic while loop. The while tests: if int is less than or equal to 5, then the condition returns true.int starts from 0, and int increases by 1 each time when the loop is processed.Run the above script, return the numbers 1 to 5, and then terminate.
#!/bin/bash
int=1
while(( $int<=5 ))
do
echo $int
let "int++"
done
Execute the script and output is
1
2
3
4
5
The above example uses the Bash command let, which is used to execute one or more expressions. There is no need to add $ to indicate the variable in the variable calculation.
The while loop can be used to read keyboard information.In the following example, the input information is set to the variable FILM, and press <Ctrl-D> to end the loop.
echo 'press <CTRL-D> to exit'
echo -n 'input your favorite website name: '
while read FILM
do
echo "Yes!$FILM is a good network"
done
Execute the script and output is
press <CTRL-D> to exit
input your favorite website name: linuxcommands
Yes!linuxcommands is a good network
The infinite Loop
The syntax is
while :
do
command
done
Or
while true
do
command
done
Or
for (( ; ; ))
The until loop
The while loop is perfect for a situation where you need to execute a set of commands while some condition is true.
The until loop is just the opposite of while loop.
In general, the while loop is better than the until loop, but in some cases—only in rare cases, until loop is more useful.
The syntax
until condition
do
command
done
The condition is generally a conditional expression.If the resulting value is false, given command are executed, otherwise the program jumps to the next line after the done statement.
Here is a simple example that uses the until loop to display the numbers zero to nine.
#!/bin/bash
a=0
until [ ! $a -lt 10 ]
do
echo $a
a=`expr $a + 1`
done
Upon execution, you will receive the following result
0
1
2
3
4
5
6
7
8
9
case
The case statement is multiple choice statement.You can use a case statement to match a value with a pattern. If the match is successful, execute the matching command.The case syntax is as follows:
case word in
pattern1)
command1
command2
...
commandN
;;
pattern2)
command1
command2
...
commandN
;;
esac
After the word is the keyword in, then various matching patterns follow by the in . Each pattern must end with a closing parenthesis.The word can be a variable or a constant.After the word to match the certain pattern, then all commands began to execute until ;; The word will detect every pattern that matches.Once the pattern matches, the other patterns will not be continued after all the commands in the matching pattern are executed.If there is no matching pattern, use the * to capture the word, and then execute the following commands. The following script need to input the number between 1 and 4 to match each pattern:
echo 'Input the number between 1 and 4:'
echo 'The number you input is:'
read aNum
case $aNum in
1) echo 'You choose 1'
;;
2) echo 'You choose 2'
;;
3) echo 'You choose 3'
;;
4) echo 'You choose 4'
;;
*) echo 'The number you input is not between 1 and 4'
;;
esac
Enter different number will have different results, for example:
Input the number between 1 and 4:: The number you input is: 3 You choose 3
Out of the loop
In the loop process, sometimes it is necessary to force out of the loop when the loop end condition is not reached. Shell uses two commands to achieve this: break and continue.
break
The break statement is used to terminate the execution of the entire loop.
The following example shows that the script enters an infinite loop as soon as a is greater than 5.To jump out of this loop and return to the shell prompt, we need to use the command break.
#!/bin/bash
while :
do
echo -n "Enter a number between 1 and 5:"
read aNum
case $aNum in
1|2|3|4|5) echo "The number you input is $aNum!"
;;
*) echo "The number you input is not between 1 and 5! Game over"
break
;;
esac
done
Upon execute and you will receive the following result
Enter a number between 1 and 5:4
The number you input is 4
Enter a number between 1 and 5:14
The number you input is not between 1 and 5! Game over
continue
The continue statement is similar to the break command, except that it causes the current iteration of the loop to exit, rather than the entire loop.
Edit the above example
#!/bin/bash while : do echo -n "Enter a number between 1 and 5:" read aNum case $aNum in 1|2|3|4|5) echo "The number you input is $aNum!" ;; *) echo "The number you input is not between 1 and 5!" continue echo "Game over" ;; esac done
Execute the script we found that when a number greater than 5 is entered, the loop in the above example will not end, and the statement echo “game over” will never be executed.
case … esac
The case…esac statement is very similar to the switch…case statement we have in other programming languages.The case…esac statement is a multiway branch structure.Each case branch starts with a right parenthesis.Use two semicolons ;; to express break which is to end the entire case … esac statement. Esac (that is the case in turn) is the end marker.
The syntax
case word in
pattern1)
command1
command2
command3
;;
pattern2)
command1
command2
command3
;;
*)
command1
command2
command3
;;
esac
The word is after the case, and the word can be a variable or a constant.
After the word is the keyword in, then various matching patterns follow by the in . Each pattern must end with a closing parenthesis. The pattern supports regular expressions.
For example
#!/bin/sh
site="linuxcommands"
case "$site" in
"linuxcommands") echo "www.linuxcommands.site"
;;
"google") echo "Google Search"
;;
"taobao") echo "taobao site"
;;
esac
Upon execute and you will get the result
www.linuxcommands.site