The first thing to think about is if else conditional judgment statement, the following gives a comprehensive statement:
ifcondition Thencondition istrueExecute all commandselifStatementelifCondition1 ThenCondition1 istrueExecute all commandselifStatementelifCondition2 ThenCondition2 istrueExecute all commandselifStatementelifConditionn ThenConditionn istrueExecute all commandsElseStatementElseNone of the above conditions aretrueExecute all commandsfi fi
Here the condition is used to test the conditions, probably the following forms of expression:
if test var == valueif"$pass""Tom" If test-f/file/exists to determine if a file exists if0 ]if [-f/etc/resolv.conf]
[-Z $Str] determines whether the string is empty (string length is 0)
When comparing values with = =, when comparing strings, use =, numeric comparisons, you can also use the following symbols:
if Ten equals if Ten greater than or equal if - Greater than if 6 less than equals if0 is less than if test $n-ne-1 is not equal to
If you want to determine whether a program execution exits normally:
#!/bin/bashdatestatus=$? if 0 then Echo " Successful Execution " Else Echo " Execution failed " fi
$? can get the return value of program execution, general 0 means success, the others are failed. So, and $? Similar to the following:
$* gets all parameters of the execution command, and the delimiter is set to the system. [Email protected] Gets all parameters of the execution command, separated by a space. $# holds the number of positional parameters.$- holds flags supplied to the Shell.$$ holds the process number of the shell (current shell). $ last Background command.
The summary of the documents is summarized as follows:
[-a/etc/resolv.conf] file exists [-e/tmp/Test.txt] file exists [-b/dev/zero] File exists and is a block specialfile[ -C/DEV/TTY0] File exists and is a character specialfile[ -D"$DEST"] folder exists [-f/path/to/file] File exists and is an ordinary file [-u/path/to/file] File exists, and set-user-ID is set[-g/path/to/file] File exists, and set-group-ID is set[-h/path/to/file] File exists, and is a symbolic link[-k/path/to/file] File exists, and the sticky bit has been set [-p/path/to/file] File exists and is a named pipe (FIFO) [-r/path/to/file] File exists and is a read-only file[ -W/path/to/file] File exists and the file is writable [-x/path/to/file] File exists, and executable
[-s/path/to/file] files exist and file size is greater than 0[-t/path/to/"True if file descriptor fd is open and refers to a terminal
[ -o/path/to/file] Trueif fileexists and is owned by the effective userID
[ -g/path/to/file] Trueif fileexists and is owned by the effective groupID
[ -l/path/to/file] Trueif fileexists and is a symbolic link
[ -s/path/to/file] Trueif fileexists and is a socket
[ -n/path/to/file] Trueif fileexists and have been modified since it was LastRead.
Finally make a small summary, the overall condition judgment is divided into integer comparison, string comparison and file comparison, although the comparative condition of the document is more, but the commonly used may not be many, and many of the letters of the comparison symbol can see its comparative meaning.
Conditional judgment of Linux shell scripts