Usage of linux shell if

an introduction 


str1 = str2 True when the two strings have the same content and length 
str1 != str2 True when the strings str1 and str2 are not equal 
-n str1 True when the length of the string is greater than 0 (string is not empty) 
-z str1 When True if the length of the string is 0 (empty string) 
str1 True if the string str1 is not empty

[ "2006.01.23" \> "2005.03.01" ] && echo dayu || echo budayu

int1 -eq int2 true if two numbers are equal 
int1 -ne int2 true if two numbers are not equal 
int1 -gt int2 true if int1 is greater than int2 
int1 -ge int2 int1 if greater than or equal to int2 true 
int1 -lt int2 int1 less than int2 true 
int1 -le int2 int1 less than or equal to int2 is true

-r file user-readable as true 
-w file user-writable as true 
-x file user-executable as true 
-f file file is regular file is true 
-d file file is directory true 
-c file file is character special file is true 
-b file file is a block special file true 
-s file true if the file size is non-zero 
-t file true when the device specified by the file descriptor (default 1) is a terminal

-a and 
-o or 
! not


The above three are written in parentheses, and the corresponding && || are written between square brackets. For example, if [ "$a" eq 1 -o "$b" eq 2 ] && [ "$c" eq 3 ]

Two specific use

The way to compare two strings for equality is:

    if [ "$test"x = "test"x ]; then

    The key points here are:

    1 Use a single equal sign

    2 Notice that there is a space on each side of the equal sign: this is a requirement of the unix shell

    3 Note the x at the end of "$test"x, which is deliberately arranged, because when $test is empty, the above expression becomes x = testx, which is obviously not equal. And without this x, the expression will report an error: [: =: unary operator expected

    

    Binary comparison operators, compare variables or compare numbers. Note the difference between numbers and strings.

    整数比较 需要注意的是 要么使用[]和gt组合 要么使用大于号和双括号组合

    -eq 等于,如:if [ "$a" -eq "$b" ]

    -ne 不等于,如:if [ "$a" -ne "$b" ]

    -gt 大于,如:if [ "$a" -gt "$b" ]

    -ge 大于等于,如:if [ "$a" -ge "$b" ]

    -lt 小于,如:if [ "$a" -lt "$b" ]

    -le 小于等于,如:if [ "$a" -le "$b" ]

     大于(需要双括号),如:(("$a" > "$b"))

    >= 大于等于(需要双括号),如:(("$a" >= "$b"))

    小数据比较可使用AWK

    字符串比较

    = 等于,如:if [ "$a" = "$b" ]

    == 等于,如:if [ "$a" == "$b" ],与=等价

     注意:==的功能在[[]]和[]中的行为是不同的,如下:

     1 [[ $a == z* ]] # 如果$a以"z"开头(模式匹配)那么将为true

     2 [[ $a == "z*" ]] # 如果$a等于z*(字符匹配),那么结果为true

     3

     4 [ $a == z* ] # File globbing 和word splitting将会发生

     5 [ "$a" == "z*" ] # 如果$a等于z*(字符匹配),那么结果为true

     一点解释,关于File globbing是一种关于文件的速记法,比如"*.c"就是,再如~也是.

     但是file globbing并不是严格的正则表达式,虽然绝大多数情况下结构比较像.

    != 不等于,如:if [ "$a" != "$b" ]

     这个操作符将在[[]]结构中使用模式匹配.

     大于,在ASCII字母顺序下.如:

     if [[ "$a" > "$b" ]]

     if [ "$a" \> "$b" ]

     注意:在[]结构中">"需要被转义.

     具体参考Example 26-11来查看这个操作符应用的例子.

    -z 字符串为"null".就是长度为0.

    -n 字符串不为"null"

     注意:

     使用-n在[]结构中测试必须要用""把变量引起来.使用一个未被""的字符串来使用! -z

     或者就是未用""引用的字符串本身,放到[]结构中。虽然一般情况下可

     以工作,但这是不安全的.习惯于使用""来测试字符串是一种好习惯.

if判断式
if [ 条件判断一 ] && (||) [ 条件判断二 ]; then
elif [ 条件判断三 ] && (||) [ 条件判断四 ]; then
else
   执行第三段內容程式
fi

例如:

 

root@Bizbox:~# a=0
root@Bizbox:~# b=0
root@Bizbox:~# c=5         
root@Bizbox:~# if [ $a = 0 -a $b = 0 ]&&[ $c != 0 ]; then
> echo success
> fi
success


if 使用的表达式

if 语句格式
if  条件
then
 Command
else
 Command
fi           别忘了这个结尾 If语句忘了结尾fi

if 的三种条件表达式
if
command
then

if
 函数
then  命令执行成功,等于返回0 (比如grep ,找到匹配)
执行失败,返回非0 (grep,没找到匹配) if [ expression_r_r_r  ]
then   表达式结果为真,则返回0,if把0值引向then if test expression_r_r_r
then  表达式结果为假,则返回非0,if把非0值引向then 
        

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324817807&siteId=291194637