Comparison in bash shell

Source: Internet
Author: User


1. Arithmetic comparison


Operator Describe

-eq

Equals
-ne
Not equal to
-gt Greater than
-lt Less than
-ge Greater than or equal
-le Less than or equal

Example:

[$var-eq 1] #变量等于1 return True

[$var-ne 1] #变量不等于1 return True

[$var-GT 1] #变量大于1 return True

[$var-lt 1] #变量小于1 return True

[$var-ge 1] #变量大于等于1 return True

[$var-le 1] #变量小于等于1 return True


2. String comparison

string comparison time using [[]] double brackets

The direct example illustrates:

[[$str 1 = = $str 2]] #字符串相同, return True

[[$str 1! = $str 2]] #字符串不相同, return True

[[$str 1 > $str 2]] #字符串str1的ascii码表序比str2大, return True

[[$str 2 < $STR 2]] #字符串str1的ascii码表序比str2小, return True

[-Z $str]] #字符串是空字符串, return True

[[Email protected] test]# [[-Z] "]];echo $? 0

[-N $str]] #字符串是非空字符串, return True

[[Email protected] test]# [[-N ' Sdfas ']];echo $?] 0


3. File System attribute Comparison

Direct illustrative examples

[-F $var] #变量为文件, return True

[-D $var] #变量为目录, return True

[-e $var] #变量所示的文件存在, return True

[-X $var] #变量所示的文件可以执行, return True

[-W $var] #变量所示文件可写, returns True

[-R $var] #变量所示文件可读, returns True

[Email protected] test]# ls-ltotal 4-rw-r--r--. 1 root root 1 23:47 test.sh[[email protected] test]# [[-X test.sh]];echo $? #因为test. SH does not have execute permissions, so $?   Not equal to 01[[email protected] test]# [[-W test.sh]];echo $?] 0[[email protected] test]# [[-R test.sh]];echo $?0

[-L $var] #变量所示是符号链接, return True

[-B $var] #变量为块设备文件, returns True

[-C $var] #变量为字符设备文件, returns True

[[Email protected] dev]# ls -l |grep cdrlrwxrwxrwx. 1 root root            3 Jan  1 21:34  cdrom1 -> sr0lrwxrwxrwx. 1 root root        &NBSP;&NBSP;&NBSP;&NBSP;3&NBSP;JAN&NBSP;&NBSP;1&NBSP;21:34&NBSP;CDRW1&NBSP;-&GT;&NBSP;SR0CRW-RW----.  1  root cdrom    21,   1 Jan  1 21:34  SG1BRW-RW----+ 1 root cdrom    11,   0 jan  1  21:34 sr0[[email protected] dev]# [[ -l cdrom1 ]];echo $?0[[ email protected] dev]# [[ -b cdrom1 ]];echo $?  #cdrom1是符号链接指向块设备, So it's true 0[[email protected] dev]# [[ -b sr0 ]];echo $?    .   0[[email protected] dev]# [[ -c sg1 ]];echo $? 0 

The above is the comparison of common operations in bash shell. Can be easily used in scripts.

This article from the "Ha-ha" blog, please be sure to keep this source http://10452483.blog.51cto.com/10442483/1730877

Comparison in bash shell

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.