- 工信部備案號 滇ICP備05000110號-1
- 滇公安備案 滇53010302000111
- 增值電信業務經營許可證 B1.B2-20181647、滇B1.B2-20190004
- 云南互聯網協會理事單位
- 安全聯盟認證網站身份V標記
- 域名注冊服務機構許可:滇D3-20230001
- 代理域名注冊服務機構:新網數碼
Bash 支持很多運算符,包括算數運算符、關系運算符、布爾運算符、字符串運算符和文件測試運算符。
原生bash不支持簡單的數學運算,但是可以通過其他命令來實現,例如 awk 和 expr,expr 最常用。
expr 是一款表達式計算工具,使用它能完成表達式的求值操作。
例如,兩個數相加:
#!/bin/bashval=`expr 2 + 2`echo "Total value : $val"
運行腳本輸出:
Total value : 4
兩點注:
表達式和運算符之間要有空格,例如 2+2 是不對的,必須寫成 2 + 2,這與我們熟悉的大多數編程語言不一樣。
完整的表達式要被 ` ` 包含,注意這個字符不是常用的單引號,在 Esc 鍵下?。
先來看一個使用算術運算符的例子:
#!/bin/sha=10b=20val=`expr $a + $b`echo "a + b : $val"val=`expr $a - $b`echo "a - b : $val"val=`expr $a * $b`echo "a * b : $val"val=`expr $b / $a`echo "b / a : $val"val=`expr $b % $a`echo "b % a : $val"if [ $a == $b ]thenecho "a is equal to b"fiif [ $a != $b ]thenecho "a is not equal to b"fi
運行結果:
a + b : 30 a - b : -10 a * b : 200 b / a : 2 b % a : 0 a is not equal to b
注意:
乘號(*)前邊必須加反斜杠()才能實現乘法運算;
if...then...fi 是條件語句?后續將會講解。
運算符 | 說明 | 舉例 |
---|---|---|
+ | 加法 | `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 ]。
關系運算符只支持數字,不支持字符串,除非字符串的值是數字。
先來看一個關系運算符的例子:
#!/bin/sha=10b=20if [ $a -eq $b ]thenecho "$a -eq $b : a is equal to b"elseecho "$a -eq $b: a is not equal to b"fiif [ $a -ne $b ]thenecho "$a -ne $b: a is not equal to b"elseecho "$a -ne $b : a is equal to b"fiif [ $a -gt $b ]thenecho "$a -gt $b: a is greater than b"elseecho "$a -gt $b: a is not greater than b"fiif [ $a -lt $b ]thenecho "$a -lt $b: a is less than b"elseecho "$a -lt $b: a is not less than b"fiif [ $a -ge $b ]thenecho "$a -ge $b: a is greater or equal to b"elseecho "$a -ge $b: a is not greater or equal to b"fiif [ $a -le $b ]thenecho "$a -le $b: a is less or equal to b"elseecho "$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/sha=10b=20if [ $a != $b ]thenecho "$a != $b : a is not equal to b"elseecho "$a != $b: a is equal to b"fiif [ $a -lt 100 -a $b -gt 15 ]thenecho "$a -lt 100 -a $b -gt 15 : returns true"elseecho "$a -lt 100 -a $b -gt 15 : returns false"fiif [ $a -lt 100 -o $b -gt 100 ]thenecho "$a -lt 100 -o $b -gt 100 : returns true"elseecho "$a -lt 100 -o $b -gt 100 : returns false"fiif [ $a -lt 5 -o $b -gt 100 ]thenecho "$a -lt 100 -o $b -gt 100 : returns true"elseecho "$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/sha="abc"b="efg"if [ $a = $b ]thenecho "$a = $b : a is equal to b"elseecho "$a = $b: a is not equal to b"fiif [ $a != $b ]thenecho "$a != $b : a is not equal to b"elseecho "$a != $b: a is equal to b"fiif [ -z $a ]thenecho "-z $a : string length is zero"elseecho "-z $a : string length is not zero"fiif [ -n $a ]thenecho "-n $a : string length is not zero"elseecho "-n $a : string length is zero"fiif [ $a ]thenecho "$a : string is not empty"elseecho "$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。 |
文件測試運算符用于檢測 Unix 文件的各種屬性。
例如,量 file 表示文件“/var/www/tutorialspoint/unix/test.sh”,它的大小為100字節,具有 rwx 權限。下面的代碼,將檢測該文件的各種屬性:
#!/bin/shfile="/var/www/tutorialspoint/unix/test.sh"if [ -r $file ]thenecho "File has read access"elseecho "File does not have read access"fiif [ -w $file ]thenecho "File has write permission"elseecho "File does not have write permission"fiif [ -x $file ]thenecho "File has execute permission"elseecho "File does not have execute permission"fiif [ -f $file ]thenecho "File is an ordinary file"elseecho "This is sepcial file"fiif [ -d $file ]thenecho "File is a directory"elseecho "This is not a directory"fiif [ -s $file ]thenecho "File size is zero"elseecho "File size is not zero"fiif [ -e $file ]thenecho "File exists"elseecho "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。 |
售前咨詢
售后咨詢
備案咨詢
二維碼
TOP