Test options and test commands for shell notes

Source: Internet
Author: User
Test options and test commands for shell notes: Shell code #! /Bin/bashwww.2cto. comecho file comparison operator echo operator description example echo-efilename if filename exists,... Info &
Test options and test commands for shell notes: Shell code #! /Bin/bash www.2cto.com echo file comparison operator echo operator description example echo-e filename if filename exists, it is true [-e/var/log/syslog] echo-d filename if filename is a directory, it is true [-d/tmp/mydir] echo-f filename. if filename is a regular file, it is true [-f/usr/bin/grep] echo-L filename. if filename is a symbolic link, true [-L/usr/bin/grep] echo-r filename if filename is readable, true [-r/var/log/syslog] echo-w filename if filename is writable, it is true [-w/var/mytmp.txt] echo- X filename if filename is executable, true [-L/usr/bin/grep] echo filename1-nt filename2 if filename1 is newer than filename2, the true [/tmp/install/etc/services-nt/etc/services] echo filename1-ot filename2 if filename1 is older than filename2, returns the true [/boot/bzImage-ot arch/i386/boot/bzImage] echo string comparison operator echo-z string. if the string length is zero, true [-z "$ myvar"] echo-n string if the string length is not zero, true [-n "$ myvar"] echo string1 = string2 if strin If g1 is the same as string2, it is true ["$ myvar" = "one two three"] echo string1! = String2 if string1 is different from string2, it is true ["$ myvar "! = "One two three"] echo arithmetic comparison operator echo num1-eq num2 equals [3-eq $ mynum] echo num1-ne num2 not equal to [3-ne $ mynum] echo num1-lt num2 less than [3 -lt $ mynum] echo num1-le num2 less than or equal to [3-le $ mynum] echo num1-gt num2 greater than [3-gt $ mynum] echo num1-ge num2 greater than or equal to [3-ge $ mynum] use the test command and [[] to perform more complex tests: shell code #! /Bin/bash www.2cto.com # test expr and [expr] are equivalent; $? Check the return value. you can use & | test 3-gt 4 & echo true | echo false #-eq,-ne,-lt,-le, -gt,-ge compares arithmetic values. # The Operator = ,! =, <,> Compare whether the strings are equal, not equal, or the first string is sorted before or after the second string. # Use the single object operator-z to test the null string; # Use the \ <\> escape to escape the string because shell also uses the <> operator for redirection. ["Abc "! = "Def"]; echo $? ["Abc" \ <"AB"]; echo $? ["Abc" \> "abcd"]; echo $? The options www.2cto.com #-a AND-o allow the use of logical operators and or to combine expressions. Single object operator! Is not logical. # Shell usually needs to run the expressions in parentheses in the subshell. Therefore, use \ (\) to escape the parentheses, or enclose these operators in single or double quotation marks. # Test "a" = "a"-a 3-lt 4; echo $? Test ""! = "B"-a 3-ge 4; echo $? [! \ ("A" = "a"-o 3-lt 4 \)]; echo $? [! \ ("A" = "a"-o 3-lt 4 ")"]; echo $? # (And [[# (expr) Composite Command to calculate the arithmetic expression. if the expression evaluates to 0, it is set to 1; # if the evaluation value is not 0, set to 0. # You do not need to escape the operators. It can perform common arithmetic, logical, and bit operations in C language. # Arithmetic operations only apply to integers. Let x = 2 y = 2 ** 3 z = y * 3 echo $ x $ y $ z (w = y/x + ((~ ++ X) & 0xf); echo $? $ X $ y $ w # (w = y/x + ((~ ++ X) & 0xf); echo $? $ X $ y $ w # [[] can use a more natural syntax for file names and strings. [[(-D "'pwd'") & (-w "'pwd'")] & echo "'pwd' is writable directory" # use = OR! = When the operator is used, it matches with the command [[and can also perform pattern matching on strings. [["Abc def. d, x --" = a [abc] * \? D *]; echo pattern match $? [["Abc def c --" = a [abc] * \? D]; echo pattern match $? [["Abc def d, x" = a [abc] * \ d *]; echo pattern match $?
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.