1.if statement
In Shell scripts, it is a common requirement to determine whether a condition is true. In Bash, you can use the following statement to implement conditional judgment:
1.1 if
if
The statement if
is the keyword of the conditional judgment statement, and its grammatical structure is:
if [ condition ]
then
# do something if condition is true
fi
[ condition ]
is a conditional expression, usually combined using comparison operators ( <
, >
, <=
, >=
, =
, !=
) and logical operators ( &&
, ). ||
Then, between then
and fi
is a piece of code to be executed when the condition is true, which can be a command or a multi-line statement block.
For example, here is a sample script that prints a message when the value of an integer variable num
is greater than 10:
#!/bin/bash
num=15
if [ $num -gt 10 ]
then
echo "num value is $num"
fi
output
num value is 15
1.2 if-else
if-else
Statements can be used to perform some actions if a condition is true and other actions if the condition is not true. The grammatical structure is:
if [ condition ]
then
# do something if condition is true
else
# do something if condition is false
fi
For example, here is a sample script that, depending on num
the value of an integer, determines its parity and prints a message:
#!/bin/bash
num=7
if [ $((num % 2)) -eq 0 ]
then
echo "The number is even"
else
echo "The number is odd"
fi
output
The number is odd
1.3 if-elif-else
if-elif-else
Statements can be used to select a condition to operate on among multiple conditions. The grammatical structure is:
if [ condition1 ]
then
# do something if condition1 is true
elif [ condition2 ]
then
# do something if condition2 is true
else
# do something if all conditions are false
fi
For example, the following is a sample script that determines num
which interval the value of an integer variable is in, and then prints the corresponding message:
#!/bin/bash
num=25
if [ $num -lt 20 ]
then
echo "The number is less than 20"
elif [ $num -ge 20 ] && [ $num -lt 30 ]
then
echo "The number is between 20 and 29"
else
echo "The number is greater than or equal to 30"
fi
output
The number is between 20 and 29
In short, in Bash, you can use if
, if-else
, if-elif-else
and other statements to make conditional judgments, and select corresponding statements to write according to different application requirements.
2. switch statement
switch
Statements, also called statements, in shell scripts case
can match different blocks of code based on one or more values. Following is switch
the basic syntax of the statement:
case expression in
pattern1)
# 匹配 pattern1 时执行的代码
;;
pattern2|pattern3)
# 匹配 pattern2 或 pattern3 时执行的代码
;;
*)
# 如果所有模式都不匹配时执行的代码
;;
esac
case
The execution of the statement is:
case
The statementexpression
tests each pattern one by one against the value ofpattern
.- If the pattern
expression
matches, the block of code associated with it is executed, and other patterns and code are skipped. - If no pattern is matched,
*
the block of code in the pattern is executed. - Two semicolons are required after each code block to stop any matching process of the pattern.
Here is an case
example of a usage statement:
#!/bin/bash
echo "input string"
read str
case $str in
"apple")
echo "苹果"
;;
"banana")
echo "香蕉"
;;
"cherry"||"cranberry")
echo "樱桃"
;;
*) # 匹配除了上面所列所有情况外的任何字符
echo "无法识别的水果"
;;
esac
echo -e "\n"
echo "input number"
read num
case $num in
1)
echo "number 1"
;;
2)
echo "number 2"
;;
3)
echo "number 3"
;;
*)
echo "other number"
;;
esac
The above example will prompt the user to enter a string, and then match the code segment associated with the entered string.
In short, switch
a statement is a flow control statement used in Shell scripts to match different code blocks based on one or more values, which can be used to simplify code and improve readability.