(go) Shell script file test operators and integer comparators

Source: Internet
Author: User

Shell script file test operators and integer comparators

Original: http://www.cnblogs.com/Steward-Xu/p/6722592.html

First, the file test operator:

In writing the test expression is, you can use the file to test the operator.

  

  More parameters can help test or man bash

Second, the string test operator:

  The role of the string test operator is to compare whether two strings are the same, whether the character length is 0, whether the string is null (NOTE: Bash distinguishes between length strings and empty strings)

"=" compares two strings in the same way as "= =", such as if ["$a" = "$b"], where a variable of $ A is best enclosed in "" because if there are spaces in the middle there will be an error. A better approach is if ["${a}" = "${b}"].

'! = ' compares two strings for the same and is true for different characters.

The writing expression is available with the following test action symbols:

  

Three, integer two-dollar comparison operator:

It contains:-eq,-ne,-gt,-ge,-lt, and-le used in [] the comparison character

= =,! =, >, >=, <, <= used in () and [[]]

  

Four: numerical calculation of variables: Includes: "^", "! =", and assignment operations.   

  

  Practical Examples:

Examples of multi-condition string tests:

  
 1 [[email protected]/]# [-Z "$file 1"] && echo ture | | echo False 2 ture 3 [[email protected]/]# [-N "$file 1"] && echo ture | | Echo False 4 False 5 [[[Email protected]/]# [-z] $file 1 "-a-z" $file 2 "] && echo ture | | echo false 6 ture 7 [[email protected]/]# [-N "$file 1"-a-n "$file 2"] && echo ture | | Echo false 8 False 9 [[email protected]/]# [-N "$file 1"-o-n "$file 2"] && echo ture | | echo False False11 [[email protected]/]# [-N "$file 1"-o-z "$file 2"] && echo ture | | echo False Ture13 [[[Email protected]/]# [["$file 1" = "$file 2"]] && echo true | | echo False True15 [[email protected]/]# [["$file 1"! = "$file 2"]] && echo true | | echo False16 false17 [[email protected]/]# [[-N $file 1 &&-n $file 2]] && echo true | | echo False18 false19 [[[Email protected]/]# [[-N $file 1 | |-n $file 2]] && echo true | | echo False False21 [[email protected]/]# [[-N $file 1 | |-Z $file 2]] && echo true | | Echo False22 True

    Examples of integer tests:

  

  
1 [[Email protected]/]# a1=12 2 [[email protected]/]# a2=13 3 [[email protected]/]# [$a 1 = $a 2] && echo True || Echo False 4 false 5 [[email protected]/]# [$a 1! = $a 2] && echo true | | Echo False 6 True 7 [[email protected]/]# [$a 1-le $a 2] && echo true | | echo False   8 true 9 [[email protected]/]# [$a 1-ge $a 2] && echo true | | echo false FALSE11 [email prote CTED]/]# [$a 1-gt $a 2] && echo true | | echo False False13 [[email protected]/]# [$a 1-lt $a 2] && echo true | | echo False TRUE15 [[email protected]/]# [$a 1-eq $a 2] && echo true | | echo False  false17 [[email protected]/]# echo $a 1 $a 218 12 13

(go) Shell script file test operators and integer comparators

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.