First, Introduction
The shell's various judgment structures and operators are based on shell programming, understanding shell judgments, operators, and some exit states
Have a very important influence on the later study. The shell has an internal command test that is often used to judge statements
To test whether the condition of one or several states is established
two. Judging conditions
(1) Test commands in the shell of Linux for testing a condition or whether a certain condition exists
The test command is a tool for judging the condition test in the statement and the Loop statement, which is helpful to the comparison test of the judgment and the operator.
(2) test condition is true, return a 0 value; False, returns a non-0 integer value
test commands in two ways, one expression (expressions); another command format [expression]
where "[" is the start Test command, "]" to be paired with, and "[" and "]" before and after the space is necessary
This method is often used as the judging condition of the Process Control statement.
2.1 String Judgments
str1 = str2 When two strings have the same content, length is true
str1! = str2 True when string str1 and str2 are not equal
- N str1 True when the length of the string is greater than 0 (string non-null)
-Z str1 True when the string length is 0 (empty string)
This place is necessary to give a small example, when we program often do some use like to use space to indicate empty
But the shell will be judged as a character, such as:
[-N "] This value echo $? Returns 0, indicating that the string is not empty.
[-Z "] This value echo $? will return non-null, indicating that it is not empty.
2.2 Number of judgements
int1-eq int2 Two numbers equal to True
Int1-ne int2 Two number is true
int1-gt Int2 int1 greater than Int2 is true
Int1-ge Int2 int1 greater than or equal to Int2 true
int1-lt Int2 int1 less than Int2 is true
Int1-le Int2 int1 less than or equal to Int2 true
2.3 Judgment of the document
-E file is true if it exists
-D file if it exists and is a directory, true
-B file if it exists and is a block special file, True
- C fileTrue if the file exists and is a character special file
- F fileTrue if the file exists and is a rule file
- G fileIf the file exists and is setThe value of the Sgid bit is true
- H fileTrue if the file exists and is a compliant link
- k fileIf the file exists and is setThe value of the "sticky" bit
- P fileTrue if the file exists and is a named pipe
- R fileTrue if the file exists and is readable
- S fileTrue if the file exists and its size is greater than 0
- u fileIf the file exists and is setsuid bit, it is true
- W fileTrue if the file exists and is writable
- x FileTrue if the file exists and can be executed
- o fileIf the file exists and is a valid userID is owned, it is true
2.4 Logical Judgment
!expr if expr is false, the compound expression is true, and expr can be any valid test expression
expr1-a expr2 if expr1 and Expr2 are true, the whole is true.
expr1-o expr2 if expr1 and Expr2 have a true-to-real
Supplemental: System Variables
$n The variable corresponds to the argument that the script was activated with
. N is a positive integer that corresponds to the position of the parameter ($1,$2 ...)
$? Exit status after the previous command was executed
$# provides the parameter number of the script
$* All of these parameters are enclosed in double quotation marks. If a script receives two parameters,$* equals $1$2 .
shell script, this is the path to the activated command
[email protected] All of these parameters are enclosed in double quotation marks, respectively. If a script receives two parameters, [email protected] is equivalent to $1$2
$$ current shell process number. For shell scripts, this is the process ID
$! when it is executing the process number of the previous background command
3. Exit status
(1) Linux system, each time the command executes, the system returns an exit status. If the exit status value is 0, the command runs successfully, and if the exit status value is not 0, the command fails. The exit status value of the last execution command is saved in the built-in variable "$?" In
(2) Exit command format: Exit status (Status between 0~255), return the status value when accompanied by the exit of the script, the parameter is saved in the shell variable $?.
Iii. Summary
(1) The various operators of the shell are important aspects of programming. If and case conditions determine the application of the structure, so that the problem has a multi-branch choice.
(2) This article simply introduces the important basic knowledge, if you want to use it skillfully, you need a lot of practice.
Conditional judgment grammar and judging condition in shell