shell中的運算子
阿新 • • 發佈:2020-09-02
運算子
Bash 支援很多運算子,包括算數運算子、關係運算符、布林運算子、字串運算子和檔案測試運算子。
原生bash不支援簡單的數學運算,但是可以通過其他命令來實現,例如 awk 和 expr,expr 最常用。
expr 是一款表示式計算工具,使用它能完成表示式的求值操作。
例如,兩個數相加:
#!/bin/bash
val=`expr 2 + 2`
echo "Total value : $val"
執行指令碼輸出:
Total value : 4
兩點注意:
- 表示式和運算子之間要有空格,例如 2+2 是不對的,必須寫成 2 + 2,這與我們熟悉的大多數程式語言不一樣。
- 完整的表示式要被
算術運算子
先來看一個使用算術運算子的例子:
#7、+、-、*、/、%:取餘數 $[] $(()) expr let bc #計算小數 # 示例 [root@Centos7 test]# n=10 [root@Centos7 test]# echo $[$n+1] [root@Centos7 test]# echo $[$n + 1] 11 [root@Centos7 test]# echo $[$n-1] 9 [root@Centos7 test]# echo $[$n/3] #忽略小數 3 [root@Centos7 test]# echo $[$n%3] #取餘數 1 [root@Centos7 test]# echo $[$n*3] 30 [root@Centos7 test]# echo $(($n+1)) 11 [root@Centos7 test]# echo $(($n-1)) 9 [root@Centos7 test]# echo $(($n/3)) 3 [root@web01 mnt]# echo $(($n*3)) 30 [root@Centos7 test]# echo $(($n%3)) 1 [root@Centos7 test]# expr $n + 1 #必須加上空格 11 [root@Centos7 test]# expr $n / 3 3 [root@web01 mnt]# expr $n \* 3 #必須加上轉義符 30 [root@Centos7 test]# expr $n+1 # 一定記得在運算子左右兩側加空格 = echo 10+1 # let運算子 [root@Centos7 test]# age=18 [root@Centos7 test]# age=$[$age+1] [root@Centos7 test]# echo $age 19 [root@Centos7 test]# let age=age+1 #let支援不呼叫,直接計算 [root@Centos7 test]# echo $age 20 [root@Centos7 test]# let age+=1 # 等於 age=age+1 [root@Centos7 test]# echo $age 21 [root@Centos7 test]# unset m #取消變數定義 [root@Centos7 test]# unset n [root@Centos7 test]# unset x [root@Centos7 test]# unset y [root@Centos7 test]# let x=m++ #賦值 [root@Centos7 test]# let y=++n #先自增,後賦值 [root@Centos7 test]# echo $x 0 [root@Centos7 test]# echo $y 1 #小數運算 [root@web01 mnt]# echo "scale=2;33/100"|bc .33 [root@Centos7 test]# echo $(echo "scale=2;33/100" | bc |cut -d. -f2)% 33%
運算子 | 說明 | 舉例 |
---|---|---|
+ | 加法 | expr $a + $b 結果為 30。 |
- | 減法 | expr $a - $b 結果為 10。 |
* | 乘法 | expr $a \* $b 結果為 200。 |
/ | 除法 | expr $b / $a 結果為 2。 |
% | 取餘 | expr $b % $a 結果為 0。 |
= | 賦值 | a=$b 將把變數 b 的值賦給 a。 |
== | 相等。用於比較兩個數字,相同則返回 true。 | [ $a == $b ] 返回 false。 |
!= | 不相等。用於比較兩個數字,不相同則返回 true。 | [ $a != $b ] 返回 true。 |
注意:條件表示式要放在方括號之間,並且要有空格,例如 [$a==$b] 是錯誤的,必須寫成 [ $a == $b ]。
浮點型
#判斷數值是否為浮點型
[ 'echo 1.1 > 0.9 |bc' -eq 1 ]
關係運算符
關係運算符只支援數字,不支援字串,除非字串的值是數字。
先來看一個關係運算符的例子:
#!/bin/sh
a=10
b=20
if [ $a -eq $b ]
then
echo "$a -eq $b : a is equal to b"
else
echo "$a -eq $b: a is not equal to b"
fi
if [ $a -ne $b ]
then
echo "$a -ne $b: a is not equal to b"
else
echo "$a -ne $b : a is equal to b"
fi
if [ $a -gt $b ]
then
echo "$a -gt $b: a is greater than b"
else
echo "$a -gt $b: a is not greater than b"
fi
if [ $a -lt $b ]
then
echo "$a -lt $b: a is less than b"
else
echo "$a -lt $b: a is not less than b"
fi
if [ $a -ge $b ]
then
echo "$a -ge $b: a is greater or equal to b"
else
echo "$a -ge $b: a is not greater or equal to b"
fi
if [ $a -le $b ]
then
echo "$a -le $b: a is less or equal to b"
else
echo "$a -le $b: a is not less or equal to b"
fi
執行結果:
10 -eq 20: a is not equal to b
10 -ne 20: a is not equal to b
10 -gt 20: a is not greater than b
10 -lt 20: a is less than b
10 -ge 20: a is not greater or equal to b
10 -le 20: a is less or equal to b
運算子 | 說明 | 舉例 |
---|---|---|
-eq | 檢測兩個數是否相等,相等返回 true。 | [ $a -eq $b ] 返回 true。 |
-ne | 檢測兩個數是否相等,不相等返回 true。 | [ $a -ne $b ] 返回 true。 |
-gt | 檢測左邊的數是否大於右邊的,如果是,則返回 true。 | [ $a -gt $b ] 返回 false。 |
-lt | 檢測左邊的數是否小於右邊的,如果是,則返回 true。 | [ $a -lt $b ] 返回 true。 |
-ge | 檢測左邊的數是否大等於右邊的,如果是,則返回 true。 | [ $a -ge $b ] 返回 false。 |
-le | 檢測左邊的數是否小於等於右邊的,如果是,則返回 true。 | [ $a -le $b ] 返回 true。 |
布林運算子
先來看一個布林運算子的例子:
#!/bin/sh
a=10
b=20
if [ $a != $b ]
then
echo "$a != $b : a is not equal to b"
else
echo "$a != $b: a is equal to b"
fi
if [ $a -lt 100 -a $b -gt 15 ]
then
echo "$a -lt 100 -a $b -gt 15 : returns true"
else
echo "$a -lt 100 -a $b -gt 15 : returns false"
fi
if [ $a -lt 100 -o $b -gt 100 ]
then
echo "$a -lt 100 -o $b -gt 100 : returns true"
else
echo "$a -lt 100 -o $b -gt 100 : returns false"
fi
if [ $a -lt 5 -o $b -gt 100 ]
then
echo "$a -lt 100 -o $b -gt 100 : returns true"
else
echo "$a -lt 100 -o $b -gt 100 : returns false"
fi
執行結果:
10 != 20 : a is not equal to b
10 -lt 100 -a 20 -gt 15 : returns true
10 -lt 100 -o 20 -gt 100 : returns true
10 -lt 5 -o 20 -gt 100 : returns false
運算子 | 說明 | 舉例 |
---|---|---|
! | 非運算,表示式為 true 則返回 false,否則返回 true。 | [ ! false ] 返回 true。 |
-o | 或運算,有一個表示式為 true 則返回 true。|| | [ $a -lt 20 -o $b -gt 100 ] 返回 true。 |
-a | 與運算,兩個表示式都為 true 才返回 true。&& | [ $a -lt 20 -a $b -gt 100 ] 返回 false。 |
字串運算子
先來看一個例子:
#!/bin/sh
a="abc"
b="efg"
if [ $a = $b ]
then
echo "$a = $b : a is equal to b"
else
echo "$a = $b: a is not equal to b"
fi
if [ $a != $b ]
then
echo "$a != $b : a is not equal to b"
else
echo "$a != $b: a is equal to b"
fi
if [ -z $a ]
then
echo "-z $a : string length is zero"
else
echo "-z $a : string length is not zero"
fi
if [ -n $a ]then
echo "-n $a : string length is not zero"
else
echo "-n $a : string length is zero"
fi
if [ $a ]then #判斷字串是否為空
echo "$a : string is not empty"
else
echo "$a : string is empty"
fi
執行結果:
abc = efg: a is not equal to b
abc != efg : a is not equal to b
-z abc : string length is not zero
-n abc : string length is not zero
abc : string is not empty
運算子 | 說明 | 舉例 |
---|---|---|
= | 檢測兩個字串是否相等,相等返回 true。 | [ $a = $b ] 返回 false。 |
!= | 檢測兩個字串是否相等,不相等返回 true。 | [ $a != $b ] 返回 true。 |
-z | 檢測字串長度是否為0,為0返回 true。 | [ -z $a ] 返回 false。 |
-n | 檢測字串長度是否為0,不為0返回 true。 | [ -z $a ] 返回 true。 |
str | 檢測字串是否為空,不為空返回 true。 | [ $a ] 返回 true。 |
注:
1.字串測試,一定要加上" "後在比較,如[ -n "$myvar" ],不加的話有時候會報錯
2.= 和 != 兩端一定要有空格
檔案測試運算子
檔案測試運算子用於檢測 Unix 檔案的各種屬性。
例如,變數 file 表示檔案“/var/www/tutorialspoint/unix/test.sh”,它的大小為100位元組,具有 rwx 許可權。下面的程式碼,將檢測該檔案的各種屬性:
#!/bin/sh
file="/var/www/tutorialspoint/unix/test.sh"
if [ -r $file ]
then
echo "File has read access"else
echo "File does not have read access"
fi
if [ -w $file ]
then
echo "File has write permission"
else
echo "File does not have write permission"
fi
if [ -x $file ]
then
echo "File has execute permission"
else
echo "File does not have execute permission"
fi
if [ -f $file ]
then
echo "File is an ordinary file"
else
echo "This is sepcial file"
fi
if [ -d $file ]
then
echo "File is a directory"
else
echo "This is not a directory"
fi
if [ -s $file ]
then
echo "File size is zero"
else
echo "File size is not zero"
fi
if [ -e $file ]
then
echo "File exists"
else
echo "File does not exist"
fi
執行結果:
File has read access
File has write permission
File has execute permission
File is an ordinary file
This is not a directory
File size is zero
File exists
操作符 | 說明 | 舉例 |
---|---|---|
-b file | 檢測檔案是否是塊裝置檔案,如果是,則返回 true。 | [ -b $file ] 返回 false。 |
-c file | 檢測檔案是否是字元裝置檔案,如果是,則返回 true。 | [ -b $file ] 返回 false。 |
-d file | 檢測檔案是否是目錄或軟連結,如果是,則返回 true。 | [ -d $file ] 返回 false。 |
-f file | 檢測檔案是否是普通檔案或者硬連結檔案,如果是,則返回 true。 | [ -f $file ] 返回 true。 |
-g file | 檢測檔案是否設定了 SGID 位,如果是,則返回 true。 | [ -g $file ] 返回 false。 |
-k file | 檢測檔案是否設定了粘著位(Sticky Bit),如果是,則返回 true。 | [ -k $file ] 返回 false。 |
-p file | 檢測檔案是否是管道檔案,如果是,則返回 true。 | [ -p $file ] 返回 false。 |
-u file | 檢測檔案是否設定了 SUID 位,如果是,則返回 true。 | [ -u $file ] 返回 false。 |
-r file | 檢測檔案是否可讀,如果是,則返回 true。 | [ -r $file ] 返回 true。 |
-w file | 檢測檔案是否可寫,如果是,則返回 true。 | [ -w $file ] 返回 true。 |
-x file | 檢測檔案是否可執行,如果是,則返回 true。 | [ -x $file ] 返回 true。 |
-s file | 檢測檔案是否為空(檔案大小是否大於0),不為空返回 true。 | [ -s $file ] 返回 true。 |
-e file | 檢測檔案(包括目錄)是否存在,如果是,則返回 true。 | [ -e $file ] 返回 true。 |
-L file | 監測檔案是否是軟連結檔案 | [ -L $file ] 返回true |
總結
1. test 測試表達式
2. [ 測試表達式 ]
3. [[ 測試表達式 ]]
4. ((測試表達式))
注:
一:1、2、3方式中測試表達式兩側必須有至少一個'空格',4不用
二:1、2等價,3是test的擴充套件,'支援正則',4常用於計算
三:&&、||、<、>、應用於[[]]中,而不能在[]中,在[]中常用 -a 、-o 、-lt(用於整數)、-gt(用於整數)
[]、test中使用的符號 (())、[[]]中使用
-eq :相等 ==/=
-ne :不相等 !=
-gt :大於 >
-ge :大於等於 >=
-lt :小於 <
-le :小於等於 <=
注:
1.比較符號兩端需要有空格
2.若在[]中使用>、<時,需要轉義,(一般不建議在[]中使用>、<)
3.儘量使用[] ,若在[]不行時,才使用[[]],儘量使用[]加 -eq的寫法
判斷字串是否為整數
1.去掉0-9後,看字串長度是否為0,或字串是否為空
2.expr和一個整數相加,看返回值是否為0
3.字元子串:將不是數字的刪除,看字串長度是否和原來的相等
計算字元長度
1:[root@myredhat ~]# expr length "$string"
15
2:[root@myredhat ~]# echo $string
i am a good boy
[root@myredhat ~]# echo ${string}|wc -L
15
3:[root@myredhat ~]# echo ${string} | awk '{print length($0)}'
15
4:[root@myredhat~]echo "${#string}"
注:
若是字串,wc -L 則統計字元個數
若是文字,wc -L 用於列印最長行的長度,而,wc -l 顯示一共的行數
判斷字元長度是否為0
1.-n 、-z判斷
2.字元子串 :IP=hello,[ ${#IP} -eq 0 ]
3.expr length "$IP" -eq 0
4.$(echo $IP | wc -L) -eq 0
5.$(echo $IP | awk '{print length}') -eq 0
shell中的test命令
Shell中的 test 命令用於檢查某個條件是否成立,它可以進行數值、字元和檔案三個方面的測試。
數值測試
引數 | 說明 |
---|---|
-eq | 等於則為真 |
-ne | 不等於則為真 |
-gt | 大於則為真 |
-ge | 大於等於則為真 |
-lt | 小於則為真 |
-le | 小於等於則為真 |
例如:
num1=100
num2=100
if test $[num1] -eq $[num2]
then
echo 'The two numbers are equal!'
else
echo 'The two numbers are not equal!'
fi
[] {} 都可以指定變數的界限
test可以使用[]代替
輸出:
The two numbers are equal!
字串測試
引數 | 說明 |
---|---|
= | 等於則為真 |
!= | 不相等則為真 |
-z 字串 | 字串長度偽則為真 |
-n 字串 | 字串長度不偽則為真 |
例如:
num1="100"
num2="100"
if test num1=num2
then
echo 'The two strings are equal!'
else
echo 'The two strings are not equal!'
fi #輸出:The two strings are equal!
[root@hass-11 test]# vim 6.txt
num1="100"
num2="100"
if test -z num1=num2
then
echo '1'
else
echo '2'
fi #輸出為2
[root@hass-11 test]# vim 6.txt
num1="100"
num2="100"
if test -n num1=num2
then
echo '1'
else
echo '2'
fi #輸出為1
檔案測試
引數 | 說明 |
---|---|
-e 檔名 | 如果檔案存在則為真 |
-r 檔名 | 如果檔案存在且可讀則為真 |
-w 檔名 | 如果檔案存在且可寫則為真 |
-x 檔名 | 如果檔案存在且可執行則為真 |
-s 檔名 | 如果檔案存在且至少有一個字元則為真 |
-d 檔名 | 如果檔案存在且為目錄則為真 |
-f 檔名 | 如果檔案存在且為普通檔案則為真 |
-c 檔名 | 如果檔案存在且為字元型特殊檔案則為真 |
-b 檔名 | 如果檔案存在且為塊特殊檔案則為真 |
例如:
#判斷檔案或目錄是否存在
[root@hass-11 test]# vim 6.txt
#!/bin/bash
cd /bin
if test -e ./bash
then
echo '1'
else
echo '2'
fi
#判斷檔案是否為空
[root@hass-11 test]# vim 6.txt
#!/bin/bash
if test -s /script/test/7.txt
then
echo '1'
else
echo '2'
fi #7.txt檔案為空,輸出為2
另外,Shell還提供了與( ! )、或( -o )、非( -a )三個邏輯操作符用於將測試條件連線起來,其優先順序為:“!”最高,“-a”次之,“-o”最低。例如:
[root@hass-11 test]# vim 6.txt
#!/bin/bash
if test -e ./a.txt -o ./b.txt -o ./3.txt -o ./4.txt -o ./bash
then
echo '1'
else
echo '2'
fi #只會輸出1
#多個 -a -o 的套用,可以使用()表示集合關係
#-e後面不支援接多個檔案