Shell script if condition judgment and judging condition summary

Source: Internet
Author: User
Tags locale readable sorts

label:

Transfer from: http://m.jb51.net/article/56553.htm This article mainly introduces the shell script IF condition judgment and judgment condition summary. This article first gives the syntax of IF condition judgment, and then gives the commonly used Summary of judgment conditions, friends in need can refer to
Foreword:
      No matter what programming language is inseparable from conditional judgment. Shell is no exception.


      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` #Get operating system type, my local is linux
if [$ SYSTEM = "Linux"]; then # print linux string if it is 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. Not much difference. But it is worth noting. The conditions in [] are judged.

1.String judgment

str1 = str2 为 true when two strings have the same content and length
str1! = str2 True when the strings str1 and str2 are not equal
-n str1 True when the length of the string is greater than 0 (the string is not empty)
-z str1 True when the length of the string is 0 (empty string)
str1 true if the string str1 is non-empty

2.Judgement of numbers

int1 -eq int2 two numbers are true
int1 -ne int2 two numbers are true
int1 -gt int2 int1 is 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 is true
int1 -le int2 int1 is less than or equal to int2 is true

3. Judgment of documents

-r file user readable as true
-w file User can write as true
-x file user executable is true
-f file: true if the file is a regular file
-d file: the file is a directory is true
-c file: the file is a character special file is true
-b file: file is a block special file is true
-s file True if file size is non-zero
-t file True when the device specified by the file descriptor (default is 1) is a terminal

4. Complex logic judgment

-a with
-o or
! 非

end

Although the syntax is simple, it becomes powerful when used in Shell.

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

Schedule:

[-a FILE] 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 file.
[-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 link.
[-k FILE] True if FILE exists and the sticky bit has been set.
[-p FILE] True if FILE exists and is a named pipe (F if O).
[-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 if it 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 link.
[-N FILE] True if FILE exists and has been mod if ied since it was last read.
[-S FILE] True if FILE exists and is a socket.
[FILE1 -nt FILE2] True if FILE1 has been changed more recently than FILE2, or if FILE1 exists and FILE2 does not.
[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 on.
[-z STRING] "STRING" is true if its length is zero.
[-n STRING] or [STRING] "STRING" is non-zero if true.
[STRING1 == STRING2] If 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" is 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.

Shell script IF condition judgment and judgment condition summary

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.