07-shell If condition test and comparison

Source: Internet
Author: User
Tags readable


If condition test common syntax
  • Test < testing expressions >
  • [< test expression >]
  • [[< Test expression >]]
  • (< test expression >))
Test command AND [] testing operator
    • Test < testing expressions >
    • [< test expression >]


(these two syntaxes are equivalent)


test operator full Spell Description
-D Directory File exists and is directory
-F File File exists and is a normal file
-E Exist File exists
-R Read File exists and is readable
-S Size File exists and file size is not 0
-W Write File exists and can be written
-X Exexutable File exists and is executable
-L Link File exists and is a linked file
F1-nt F2 Newer than File f1 than file F2 new
F1-ot F2 Olderthan File F1 F2 older than file
-N Not zero The length of the string is not 0
-Z Zero The length of the string is 0
S1 = s2 String 1 equals String 2
S1! = S2 String 1 is not equal to string 2
-eq Equal Equal
-ne Not equal Not equal
-gt Greater than Greater than
-ge Greater equal Greater than or equal
-lt Less than Less than
-le Less equal Less than or equal
-A and And
-O Or Or
! Not Non -
Example:
test -f /data/test.sh && echo true || echo false
[ -f /data/test.sh ] && echo true || echo false
    • When testing a variable with [], the test result may be incorrect if the variable being tested does not have double quotes:
file1 = / etc / passwd
[-f "$ file1"] && echo 1 || echo 0

If it is a file entity path, the result of quoting is the same as unquoting:
[-f "/ etc / passwd"] && echo 1 || echo 0
[-f / etc / passwd] && echo 1 || echo 0
[[]] test operator
    • [[< Test expression >]]
In [[]] you can use wildcards, etc. for pattern matching
[[! $ a = ~ [1-3]]] && echo 1 || echo 0
test operator full Spell Description
-D Directory File exists and is directory
-F File files exist and are normal files
-E Exist File exists
-R Read File exists and is readable
-S Size File exists and file size is not 0
-W Write File exists and can be written
-X Exexutable File exists and is executable
-L Link File exists and is a linked file
F1-nt F2 Newer than File f1 than file F2 new
F1-ot F2 Olderthan File F1 F2 older than file
-N Not zero The length of the string is not 0
-Z Zero The length of the string is 0
S1 = s2 String 1 equals String 2
S1! = S2 String 1 is not equal to string 2
==/= Equal Equal
!= Not equal Not equal
> Greater than Greater than
>= Greater equal Greater than or equal
< Less than Less than
<= Less equal Less than or equal
&& and And
|| Or Or
! Not Non -
(()) test operator
  • (< test expression >))
    • Common use and calculation
    • For relational operations of integers, you can also use the Shell's Arithmetic operator (())
((3>2)) && echo 1 || echo 0
((3!=2&&5==5)) && echo 1 || echo 0


07-shell If condition test and comparison


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.