Shell script if condition judgment and judgment condition summary _linux Shell

Source: Internet
Author: User
Tags locale readable sorts

Objective:
no matter what programming language is inseparable from conditional judgment. The shell is no exception.

Copy Code code as follows:

If List Then
Do something here
Elif List Then
Do another thing
Else
Do something else here
Fi

EX1:
Copy Code code as follows:

#!/bin/sh
System= ' uname-s ' #获取操作系统类型, my local Linux
if [$SYSTEM = "Linux"]; Then #如果是linux的话打印linux字符串
echo "Linux"
elif [$SYSTEM = "FreeBSD"]; Then
echo "FreeBSD"
elif [$SYSTEM = "Solaris"]; Then
echo "Solaris"
Else
echo "What?"
Fi #ifend

Basically the same as other scripting languages. There is not much difference. But it's worth noting that. [] The conditions inside are judged.

1. String judgment

STR1 = str2 When two strings have the same content, the length is true
STR1!= str2 is true when strings str1 and str2 are unequal
-N str1 True when the length of the string is greater than 0 (string not empty)
-Z str1 True when the length of the string is 0 (empty string)
STR1 is True when string str1 is Non-null

2, the number of judgments

Int1-eq int2 Two number equal to True
Int1-ne Int2 Two is not equal to the true number
INT1-GT Int2 int1 greater than Int2 is true
Int1-ge Int2 Int1 is greater than or equal to Int2 is true
Int1-lt Int2 Int1 is less than int2 true
Int1-le Int2 Int1 is less than or equal to Int2 true

3, the document judgment

-r file user readable as True
-W file user can write as true
-X file user can execute as true
-F file is true for regular files
-D file is true for directory
-C file is true for character special files
-B file is a block special file is True
-S file file non-0 o'clock is true
-T file True when the device specified by the file descriptor (default is 1) is a terminal

4. Complex logic judgment

-A and
-O or
! Non -

End

The syntax is simple, but when used in a shell, his function becomes powerful.

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

Schedule

[-A file] if FILE existence is true.
[-B-file] True if file exists and is a block-specific file.
[-C file] is true if file exists and is a word special file.
[-D file] is true if FILE exists and is a directory.
[-e-file] is true if FILE exists.
[-F file] is true if file exists and is a normal file.
[-G file] True if FILE exists and Sgid is already set. [-H file] True if FILE exists and is a symbolic connection.
[-K-file] True if file exists and a sticky bit has been set.
[-P file] If FILE exists and is a name pipe (f if O) true.
[-R File] True if FILE exists and is readable.
[-S file] If FILE exists and the size is not 0 true.
[-T FD] true if the file descriptor FD is open and points to a terminal.
[-U FILE] true if FILE exists and suid (set user ID) is set.
[-W file] If file is true if file exists and is writable.
[-X file] True if FILE exists and is executable.
[-O FILE] is true if FILE exists and is a valid user ID.
[-G file] True if FILE exists and is a valid user group.
[-L file] true if FILE exists and is a symbolic connection.
[-N file] If FILE exists and has been mod if IED since it is last read is true.
[-S file] is true if FILE exists and is a socket.
[File1-nt FILE2] If FILE1 has been changed more recently than-FILE2, or if FILE1 exists and FILE2 does is true.
[File1-ot FILE2] True if FILE1 is older than FILE2, or 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 turned on.
[-Z STRING] The length of "STRING" is zero is true.
The length of [-N string] or [string] "string" is not 0 Non-zero is true.
[STRING1 = = STRING2] If 2 strings are the same. "=" May is used instead of "= =" for strict POSIX compliance is true.
[STRING1!= STRING2] True if the string is not equal.
[STRING1 < STRING2] if "STRING1" sorts before "STRING2" lexicographically in the current locale is true.
[STRING1 > STRING2] If "STRING1" sorts after "STRING2" lexicographically to the current locale is true.
[ARG1 OP ARG2] "OP" is one of-eq,-ne,-lt,-le,-gt Or-ge. These arithmetic binary operators return true if "ARG1" are equal to, not equal to, less than, less than or equal to, great Er than, or greater than or equal to "ARG2", respectively. "ARG1" and "ARG2" are integers.

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.