Shell string comparison Equality, inequality method summary _linux Shell

Source: Internet
Author: User
#!/bin/sh #测试各种字符串比较操作. #shell中对变量的值添加单引号, the difference between the hot quotes and the not added: it is irrelevant to the type, that is, not adding quotes into the string type, #单引号不对相关量进行替换, if the $ symbol is not interpreted as a variable reference, thus substituting for the value of the corresponding variable, double quotes will be substituted Author:tenfyguo a= "$ b=" $ "$ echo" entered the original value: A= $A, b= $B "#判断字符串是否相等 if [" $A "=" $B "];then echo" [=] "fi #判断字符串是否相等, with on  face = equivalent if ["$A" = = "$B"];then echo "[=]" fi #注意: the function of = = is different in [[]] and [], as follows #如果 $a with "A" (pattern match) then will be true if [[$A] = * ]];then echo "[[==a*]]" fi #如果 $a equals a * (character match), the result is true if [["$A" = = "*"]];then echo "==/" a*/"" Fi #File globbing "(wildcard 
And word splitting will occur, and a * will automatically match to the corresponding file currently beginning with a: add_crontab.sh, the following will output OK #if ["add_crontab.sh" = = *];then #echo "OK" #fi if ["$A" = *];then echo "[==a*]" fi #如果 $a equals a * (character match), then the result is true if ["$A" = "*"];then echo "==/" * /"" "Fi #字符串不相等 If [" $A "!=" $B "];then echo" [!=] "fi #字符串不相等 if [[$A]"!= "$B"]];then echo "[[!=]]" fi #字符串不为空,
Length is not 0 if [-N "$A"];then echo "[-n]" fi #字符串为空. That's the length of 0. If [-Z "$A"];then echo "[-z]" fi #需要转义, otherwise it is considered a redirection symbol if [$A/<$B];then echo "[<]" fi if [$A < $B]];then echo [[[<]] "fi #需要转义", otherwise considered to be a redirection symbol if [$A/> $B]

 Then echo "[>]" fi if [[$A > $B]];then echo [[>]] "fi
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.