Shell script IF condition judgment and judgment condition details, shellif

Source: Internet
Author: User

Shell script IF condition judgment and judgment condition details, shellif
Preface:

No matter what programming language, conditional judgment is indispensable. SHELL is no exception.

if list thendo something hereelif list thendo another thing hereelsedo something else herefi

EX1:

#! /Bin/shSYSTEM = 'uname-S' # obtain the operating SYSTEM type. I use linuxif [$ SYSTEM = "Linux"] locally. then # If it is linux, print the linux string echo "Linux" elif [$ SYSTEM = "FreeBSD"]; thenecho "FreeBSD" elif [$ SYSTEM = "Solaris"]; thenecho "Solaris" elseecho "What? "Fi # ifend

It is basically the same as other scripting languages. There is no big difference. However, it is worth noting that. [] Condition judgment.

1. String judgment

Str1 = str2: true when two strings have the same content and length

Str1! = Str2: true when str1 and str2 are different

-N str1: true when the length of a string is greater than 0 (the string is not empty)

-Z str1: true (empty string) when the string length is 0)

Str1 is true when str1 is not empty

2. digit judgment

Int1-eq int2 two numbers are equal to true

Int1-ne int2 two numbers are not true

Int1-gt int2 int1 greater than int2 true

Int1-ge int2 int1 greater than or equal to int2 true

Int1-lt int2 int1 smaller than int2 true

Int1-le int2 int1 smaller than or equal to int2 true

3. File judgment

-R file user readable to true

-W file users can write to true

-X file user executable is true

-F file: the regular file is true.

-D file: the directory is true.

-C file: the special character file is true.

-B file: The Block special file is true.

-S file is true if the file size is not 0

-T file: this parameter is true when the specified device is a terminal in the file descriptor (1 by default ).

4. complicated logic judgment

-A and

-O or

! Non

End

Although the syntax is simple, its functions become powerful when used in SHELL.

========================================================== ==================================

Appendix:

[-A FILE] true if the FILE exists.

[-B FILE] If the FILE exists and is a special block FILE, it is true.

[-C FILE] If the FILE exists and is a special word FILE, it is true.

[-D FILE] If the FILE exists and is a directory, it is true.

[-E FILE] If the FILE exists, it is true.

[-F FILE] If the FILE exists and is a normal FILE, it is true.

[-G FILE] If the FILE exists and the SGID has been set, it is true. [-H FILE] If the FILE exists and is a symbolic connection, it is true.

[-K FILE] true if the FILE exists and the stick bits have been set.

[-P FILE] If the FILE exists and is a name pipe (F If O), it is true.

[-R FILE] true if the FILE exists and is readable.

[-S FILE] If the FILE exists and the size is not 0, it is true.

[-T FD] It is true if the file descriptor FD is opened and points to a terminal.

[-U FILE] If the FILE exists and SUID (set user ID) is set, it is true.

[-W FILE] true if the FILE exists and is writable.

[-X FILE] true if the FILE exists and is executable.

[-O file] true if the FILE exists and belongs to a valid user ID.

[-G file] true if the FILE exists and belongs to a valid user group.

[-L file] If the FILE exists and is a symbolic connection, it is true.

[-N file] If FILE exists and has been mod, it is true if ied since it was last read.

[-S file] If the FILE exists and is a socket, it is true.

[FILE1-nt FILE2] If FILE1 has been changed more recently than FILE2, or if FILE1 exists and FILE2 does not is true.

[FILE1-ot FILE2] True if FILE1 is older than FILE2, or if FILE2 exists and FILE1 does not exist.

[FILE1-ef FILE2] True if FILE1 and FILE2 point to the same device and node number.

[-O OPTIONNAME] true if the shell option "OPTIONNAME" is enabled.

[-Z STRING] "STRING" is true if its length is zero.

If the length of [-n STRING] or [STRING] "STRING" is non-zero, it is true.

[STRING1 = STRING2] If the two strings are the same. "=" May be used instead of "=" for strict POSIX compliance is true.

[STRING1! = STRING2] true if the strings are not equal.

[STRING1 <STRING2] True if "STRING1" sorts before "STRING2" lexicographically in the current locale.

[STRING1> STRING2] True if "STRING1" sorts after "STRING2" lexicographically in the current locale.

[ARG1 OP ARG2] "OP" is one of-eq,-ne,-lt,-le,-gt or-ge. these arithmetic binary operators return true if "ARG1" is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to "ARG2 ", respectively. "ARG1" and "ARG2" are integers.

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.