Linux Shell Script condition judgment

Source: Internet
Author: User
Tags locale sorts

Shell condition judgment

If List Then

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

EX1:

#!/bin/sh

System= ' uname-s ' #获取操作系统类型, I'm 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's no big difference. But it's worth noting that. [] inside the condition to judge.

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)
STR1 is True when string str1 is not empty

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

3 Judgment of the document

-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 files are directory-True
-C File file is true for character special files
-B file files are true for block special files
-S file files non-0 o'clock True
-T file is true when the specified device is terminal (default = 1)

3 Complex logic Judgments

-A and
-O or
! Non -

End

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

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

Schedule

[-A file] is true if file exists.
[-B file] True if file exists and is a block special file.
[-C file] True if file exists and is a word special.
[-D file] True if file exists and is a directory.
[-E File] True if file exists.
[-F file] True if file exists and is a normal file.
[-G file] True if file exists and Sgid has been set. [-H file] True if file exists and is a symbolic connection.
[-K file] True if file exists and the sticky bit has been set.
[-P file] If file exists and is a name pipe (f if O) is true.
[-R File] True if file exists and is readable.
[-S file] True if file exists and the size is not 0.
[-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] True if file exists and is writable.
[-X file] True if file exists and is executable.
[-o file] 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] True if file exists and is a socket.
[File1-nt FILE2] If FILE1 have been changed more recently than FILE2, or if FILE1 exists and FILE2 does not is true.
[File1-ot FILE2] If FILE1 is older than FILE2, or FILE2 exists and FILE1 does not exist, it is true.
[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] "STRING" is true if the length is zero.
[-N STRING] or [string] "string" is true if the length is not 0 non-zero.
[STRING1 = = STRING2] If 2 strings are the same. "=" May is used instead of "= =" for strict POSIX compliance is true.
[STRING1! = STRING2] True if the strings are not equal.
[STRING1 < STRING2] if "STRING1" sorts before "STRING2" lexicographically in the current locale is true.
[STRING1 > STRING2] If the ' STRING1 ' sorts after ' STRING2 ' lexicographically in 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" is 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.

Linux Shell Script condition judgment

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.