Conditional judgment of Linux shell scripts

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.