The shell has conditional test statements, which are usually done using the test command or the [] command, which is the basis of the conditional Judgment statement If~then statement, especially the [] command. Let's explain some of the conditional test statements below.
1. Test command Implementation condition testing
The test command is useful for detecting the existence of certain files in the system, or for related properties.
The basic syntax is as follows:
The test command also tests the string:
The test command also tests the relationships between integers:
2. [] command to implement condition testing
You should pay special attention when using the brackets [] command to implement the conditional test function. Because the brackets are useful in many places, such as a wildcard wildcards regular expression. When we use [] to represent condition testing, we must follow the following principles:
1) each component within the brackets [] needs to be separated by a space bar, for example:[-"$HOME"-==-"$MAIL"-]
2) in brackets, it is best to enclose the variable in double quotation marks;
3) Constants in brackets, most of which are enclosed in single or double quotation brackets.
[] command to implement conditional tests, typically used in conditional judgment statements for If~then.
Reference:
"Brother Bird's Linux private dish"
Conditional test statements in the shell