Linux operating system flow control statement - introduction to the use of if statement
This blog will introduce how to use the flow control statement-if statement in the Linux operating system. We will introduce the structure and usage of single-branch statement, double-branch statement and multi-branch statement in detail. In addition, file comparisons, integer comparisons, character comparisons, and the use of regular expressions are covered. By learning these contents, you will be able to better grasp the process control in shell scripting, and realize conditional judgment and execution of different branches.
single branch statement
A single-branch statement is the simplest form of an if statement, with only one condition and one block of execution. If the condition is true (non-zero), the commands in the block are executed.
Here is an example:
if condition
then
# 执行块中的命令
fi
double branch statement
A double branch statement is a common form of if statement with two conditions and two execution blocks. Executes different blocks of code depending on whether the condition is true or false.
Here is an example:
if condition1
then
# 执行条件1为真时的命令
else
# 执行条件1为假时的命令
fi
multi-branch statement
The multi-branch statement elif
realizes the judgment of multiple conditions and the selection of execution blocks by using keywords.
Here is an example:
if condition1
then
# 执行条件1为真时的命令
elif condition2
then
# 执行条件2为真时的命令
else
# 所有条件都为假时的命令
fi
file comparison
In shell scripts, we can use different criteria to compare the attributes and contents of files.
Here are some common file comparison criteria:
-e 文件路径
: Whether the file exists.-f 文件路径
: Whether the file is an ordinary file.-d 文件路径
: Whether the file is a directory.-r 文件路径
: Whether the file is readable or not.-w 文件路径
: Whether the file is writable.-x 文件路径
: Whether the file is executable.
Here is an example:
if [ -f "example.txt" ]
then
echo "example.txt is a regular file."
fi
integer comparison
Flow control statements in shell scripts can also be used to compare integers.
The following are some commonly used integer comparison conditions:
-eq
:equal.-ne
:not equal to.-gt
:more than the.-lt
: less than.-ge
:greater or equal to.-le
: less than or equal to.
Here is an example:
age=25
if [ $age -ge 18 ]
then
echo "You are an adult."
fi
character comparison
Flow control statements can also be used to compare strings.
The following are some common string comparison conditions:
=
:equal.!=
:not equal to.-z
: is an empty string.-n
: A non-empty string.
Here is an example:
name="John"
if [ "$name" = "John" ]
then
echo "Hello, John!"
fi
regular expression
In flow control statements, regular expressions can also be used for pattern matching.
Here is an example:
string="Hello, World!"
if [[ $string =~ ^Hello ]]
then
echo "String starts with 'Hello'."
fi
Flow control statement: regular comparison, using [[]]
When using regular expressions for comparison, you can use [[
and ]]
to wrap conditions.
Here is an example:
string="Hello, World!"
if [[ $string =~ [0-9]+ ]]
then
echo "String contains a number."
fi
script example
Below is an example script that demonstrates the use of flow control statements - if statements:
#!/bin/bash
age=25
if [ $age -ge 18 ]
then
echo "You are an adult."
else
echo "You are not an adult yet."
fi
name="John"
if [ "$name" = "John" ]
then
echo "Hello, John!"
fi
string="Hello, World!"
if [[ $string =~ ^Hello ]]
then
echo "String starts with 'Hello'."
fi
Save the above content as a if_script.sh
script file named , and grant execution permission ( chmod +x if_script.sh
). Then run the script ( ) in the terminal ./if_script.sh
to see the output.
in conclusion
This blog introduces how to use the flow control statement-if statement in the Linux operating system, including the structure and usage of single-branch statement, double-branch statement and multi-branch statement. In addition, file comparison, integer comparison, character comparison and the use of regular expressions are introduced. By learning and applying these knowledge, you can better grasp the process control in shell scripting, and realize conditional judgment and execution of different branches. Hope this blog was helpful to you!