Shell comparison (file test, string, Integer)

Source: Internet
Author: User
Tags arithmetic syslog

File testing (-f, -d, -r, -r, ...)
[-f /root/file.txt], [-d / root / mydir]
[[-f /root/file.txt]]
File testing [[]] is the same as []
String comparison (-z, -n,! =, ==, ...)
[-z "$ {str}"], ["$ {str}"! = "$ {str}"], [! -n "$ {str}"]
[] Can only be used for character comparison, not for pattern matching
[[-z "$ {str}"]], [[! -n "$ {str}"]]
[[$ a == z *]] # If $ a starts with "z" (pattern matching) then it will be true
[[hello == hell?]] # Pattern matching [[ab == "test"]] # If $ a is equal to "test", then the result is true
[[]] Can perform pattern matching, use "str" then str is treated as a string
When comparing strings, you can use the right as a pattern (this is the case where the right string is not enclosed in double quotes. If the right string is enclosed in double quotes, it is considered to be a text string.), Not just A string, such as [[hello == hell?]], Is true.
Use -a, -o for logical connection: ["$ {str1}" == "test" -a -z "$ {str}"]
Use -a, -o for logical connection: [["$ {str1}" == "test" && -z "$ {str}"]]
[] Use -a, -o, [[]] Use &&, ||
The string contains: [["$ a3" = ~ "$ a1"]]
True if a1 is included in a3
Arithmetic comparison
((4> 2)), ((4 + 2> 3))
#This arithmetic operation, integer comparison is applicable
[5 -gt 3], [5 \> 3]
[[5> 3]], [[5 -gt 3]]
[[3 + 5 -gt 2]]
Cannot use <,>, the result is often wrong
((a + b))
When the result is non-zero, return true
  [...],[[...]],((...))the difference  
In bash ((...)), it mainly performs arithmetic operations (neither of the above two), and is more suitable for integer comparison. You can directly use familiar comparison operators such as <,>. You can directly use variable names such as var without the form of $ var. Support multiple expressions separated by semicolons
Test expr and [expr] have the same effect on the command line. The three basic functions of test are to judge files, judge strings, and judge integers. Supports using NAND to connect expressions
2. The only comparison operators available in test and [] are == and! =, Both of which are used for string comparison, not for integer comparison. Integer comparison can only use the form -eq, -gt. If you compare "ab" and "bc", you must use the escape character: [ab \ <bc]
[[]], This is a command built into the shell, it supports pattern matching of strings, logical combination can use &&, || instead of test-a, -o
[...] is a shell command, so the expression in it should be its command line parameter, so the string comparison operator ">" and "<" must be escaped, otherwise it becomes the IO redirection operator. No escaping in [[中 "<" 和 ">"
Attachment: compare command
Operator Description Example
File comparison operator
-e filename True if filename exists [-e / var / log / syslog]
-d filename true if filename is a directory [-d / tmp / mydir]
-f filename True if filename is a regular file [-f / usr / bin / grep]
-L filename is true if filename is a symbolic link [-L / usr / bin / grep]
-r filename is true if filename is readable [-r / var / log / syslog]
-w filename True if filename is writable [-w /var/mytmp.txt]
-x filename True if filename is executable [-L / usr / bin / grep]
filename1 -nt filename2 True if filename1 is newer than filename2 [/ tmp / install / etc / services -nt / etc / services]
filename1 -ot filename2 True if filename1 is older than filename2 [/ boot / bzImage -ot arch / i386 / boot / bzImage]
String comparison operators (note the use of quotation marks, which is a good way to prevent spaces from disturbing the code)
-z string true if the length of string is zero [-z "$ myvar"]
-n string true if the length of string is non-zero [-n "$ myvar"]
string1 = string2 is true if string1 and string2 are the same ["$ myvar" = "one two three"]
string1! = string2 True if string1 and string2 are different ["$ myvar"! = "one two three"]
Arithmetic comparison operator
num1 -eq num2 is equal to [3 -eq $ mynum]
num1 -ne num2 is not equal to [3 -ne $ mynum]
num1 -lt num2 is less than [3 -lt $ mynum]
num1 -le num2 is less than or equal to [3 -le $ mynum]
num1 -gt num2 is greater than [3 -gt $ mynum]
num1 -ge num2 is greater than or equal to [3 -ge $ mynum]
Comparison in shell (file test, string, integer)

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.