天天看點

shell if 多個條件_shell基礎之運算符

頭條号:浩渺煙波

接着上一篇shell變量繼續往下說:

Shell中是不支援簡單數學運算的,比如這樣:

[[email protected]_0_11_centos ~]# rest=10+10[[email protected]_0_11_centos ~]# echo $rest10+10
           

但是我們可以加上指令使其支援,在shell中有三種弄方法可以支援簡單的數學運算:

  1. 使用$(())

    這個$後面是兩個小括号,一個小括号不對:

[[email protected]_0_11_centos ~]# rest=$((10+20))[[email protected]_0_11_centos ~]# echo $rest30[[email protected]_0_11_centos ~]# rest=$(10+20) -bash: 10+20: command not found
           
  1. 使用$[]

    使用$[]這個感覺是最簡單的

[[email protected]_0_11_centos ~]# rest=$[10 + 10][[email protected]_0_11_centos ~]# echo $rest20
           

3.使用關鍵字expr使用這個expr有點複雜,在使用的時候需要用反單引号将其包裹起來,而且必須有空格:

[[email protected]_0_11_centos ~]# rest=`expr 10 + 20`[[email protected]_0_11_centos ~]# echo $rest30
           

如果沒有空格的話:

[[email protected]_0_11_centos ~]# rest=`expr 10+20`[[email protected]_0_11_centos ~]# $rest-bash: 10+20: command not found
           

而且還不支援括号,并且乘号還要用反斜杠給轉譯一下:

[[email protected]_0_11_centos ~]# rest=`expr 10 + 20 * (10 + 10)` -bash: command substitution: line 1: syntax error near unexpected token `('-bash: command substitution: line 1: `expr 10 + 20 * (10 + 10)'[[email protected]_0_11_centos ~]# 
           

是以遇到複雜的運算,需要一步一步的算,很麻煩!

還有一些其他的簡單運算 比如乘除,取餘等都類似

關系運算:一般用條件語句上,需要用到[]和(())比如 大于 在[]中要用-gt表示 ,在(())用==表示,gt是 greater than的縮寫例如 在檔案中寫入:

if (($1 > $2))then        echo "$1 大于 $2"else        echo "$1 小于 $2"fiif [ $1 -gt $2 ]then        echo "$1 大于 $2"else        echo "$1 小于 $2"fi
           

執行:

[[email protected]_0_11_centos shell]# ./myShell.sh 100 200100 小于 200100 小于 200
           

shell的if語句也挺奇怪,if 之後是換行,然後then 最後需要加上fi,表示結束。 如果多個判斷的話用elif,不是else if,跟java還是有差別的需要注意的一點的是[]中括号使用的時候,一定要主要前後的空格,否則會報錯的。

其他的關系運算類似:-eq 表示相等,在(())用 ==表示,是equal的縮寫-ne表示不相等,在(())用 !=表示,是 not equal的縮寫-gt 表示大于,在(())用 >表示,是greater than的縮寫-ge 表示大于等于,在(())用 >=表示,是greater equall的縮寫-lt 表示小于,在(())用

邏輯運算符 邏輯運算符,一般是指與或非在shell []中 -a表示與,就是兩個表達式都成立,條件才成立,在(())表示為&&-o或運算,表示兩個條件隻要有一個為真即可,在(()))表示為||!表示非 在(()))表示為!

字元串運算符= 檢測兩個字元串是否相等,相等則傳回true != 檢測兩個字元串是否相等,不相等則傳回true-z 檢測字元串長度是否為0,為0則傳回true-n 檢測字元串長度是否為0,不為0則傳回true str 檢測字元串是否為null,不為null則傳回true

比如:

str="abcdee"if [ $str = "abc" ]then        echo "相等"else        echo "不相等"fiif [ -n $str ]then        echo "不為0"else        echo "為0"fi
           

執行結果:

[[email protected]_0_11_centos shell]# ./myShell.sh  不相等不為0
           

檔案檢測運算符:

-b 檢測檔案是否是塊裝置檔案,如果是,則傳回true -c 檢測檔案是否是字元裝置檔案,如果是,則傳回true -d 檢測檔案是否是目錄檔案,如果是,則傳回true -f 檢測檔案是否是普通檔案(既不是目錄也不是裝置檔案),如果是,則傳回true -g 檢測檔案是否設定了SGID位,如果是,則傳回true -k 檢測檔案是否設定了粘着位(stucky Bit),如果是,則傳回true -p 檢測檔案是否具名管道,如果是,則傳回true -u 檢測檔案是否設定了SUID位,如果是,則傳回true -r 檢測檔案是否可讀,如果是,則傳回true -w 檢測檔案是否可寫,如果是,則傳回true-x 檢測檔案是否可執行,如果是,則傳回true -s 檢測檔案是否為不為空(檔案大小是否不為0),如果不為0,則傳回true -e 檢測檔案(包括目錄)是否存在,如果存在,則傳回true -a 檢測檔案(包括目錄)是否存在,如果存在,則傳回true

比如檢測檔案是否存在用-e 比如建立一個檔案test.txt

file=/root/training/shell/test.txtif [ -e $file ]then        echo "檔案存在"else        echo "檔案不存在"fi
           

執行的結果:

[[email protected]_0_11_centos shell]# ./myShell.sh 檔案存在
           

未完待續!

參考文章:

https://blog.csdn.net/yuki5233/article/details/81166509

https://www.runoob.com/linux/linux-shell-basic-operators.html

shell if 多個條件_shell基礎之運算符