11.1 使用if-then語句
- 格式如下
- if語句會執行if行定義的那個命令,如果該命令的退出狀態碼是0,則then部分的語句就會執行,其他值,則不會
1
2
3
4
|
if command
then
commands
fi
|
- 在要執行的命令結尾加個分號,就能在同一行使用then語句了,格式如下
1
2
3
|
if command; then
commands
fi
|
11.2 if-then-else語句
- 格式如下;
1
2
3
4
5
6
|
if command
then
commands
else
commands
fi
|
11.3 嵌套if
- 格式如下:
1
2
3
4
5
6
7
|
if command1
then
commands
elif command2
then
more commands
fi
|
11.4 test命令
- 格式如下
1
|
test condition
|
- test用在if-then語句中
1
2
3
4
|
if test condition
then
commands
fi
|
- bash shell提供了另一個在if-then語句中聲明的test命令的方法
- 方括號定義了test命令中用到的條件,注意方括號左右有一個空格
1
2
3
4
|
if [ condition ]
then
commands
fi
|
- test命令可以判斷3類條件:
- 數值比較
- 字符串比較
- 文件比較
11.4.1 數值比較
- test命令數值比較功能:
比較 | 說明 |
---|---|
n1-eq n2 | 檢查n1是否與n2相等 |
n1-ge n2 | 檢查n1是否大於或等於n2 |
n1-gt n2 | 檢查n1是否大於n2 |
n1-le n2 | 檢查n1是否小於或等於n2 |
n1-lt n2 | 檢查n1是否小於n2 |
n1-ne n2 | 檢查n1是否不等於n2 |
– 數值條件測試可以用在數值和變量上
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
...
val1=10
val2=11
if [ $val1 -gt 5 ]
then
echo ...
fi
if [ $val1 -eq $val2 ]
then
echo ...
else
echo ...
fi
...
|
- test命令無法處理變量中存儲的浮點值,其處理的數僅有整數,當使用bc計數器時,可以讓shell將浮點值作為字符串存儲進一個變量
1
2
3
4
5
6
7
8
|
...
val1=`echo "scale = 4; 10 / 3" | bc`
if[ $val1 -gt 3 ]//運行腳本時會報錯
then
echo ...
fi
...
|
11.4.2 字符串比較
- test命令的字符串比較功能
比較 | 描述 |
---|---|
str1 = str2 | 檢查str1是否和str2相同 |
str1 = str2 | 檢查str1是否和str2不同 |
str1 = str2 | 檢查str1是否比str2小 |
str1 = str2 | 檢查str1是否比str2大 |
-n str1 | 檢查str1的長度是否非0 |
-z str1 | 檢查str1的長度是否為0 |
– 比較字符串相等性:會將所有的標點和大寫也考慮在內
– 字符串順序注意事項:
– 大於小於符號必須轉義,否則shell會把他們當做重定向符號而把字符串當做文件名
– 大於小於順序和sort命令所采用的不同
– 在test命令中大寫字母會被當成小於小寫字母
– test命令使用標准的ASCII順序,根據每個字符的ASCII數值來決定排序順序
– 當將同樣的字符串放進文件中並用sort命令排序時,小寫字母會先出現
– sort命令使用系統的本地化語言設置中定義的排序順
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
$cat test
#!/bin/bash
val1=baseball
val2=hockey
if [ $val1 \> $val2 ]
then
echo "$val1 is greater than $val2"
else
echo "$val1 is less than $val2"
fi
$
$./test
baseball is less than hockey
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
$cat test
#!/bin/bash
val1=Testing
val2=testing
if [ $val1 \> $val2 ]
then
echo "$val1 is greater than $val2"
else
echo "$val1 is less than $val2"
fi
$
$./test
Testing is less than testing
$sort testfile
testing
Testing
$
|
- 字符串大小
- -n和-z參數:檢查一個變量是否含有數據
1
2
3
4
5
6
7
8
9
10
11
|
val1=testing
val2=' '
if [ -n "$val1" ] //檢查val1變量是否長度非零
then
...
if [ -z "$val2" ] //檢查val2變量是否長度為零
then
...
if [ -z "$val3" ] //檢查val1變量是否長度非零,這個變量並未在shell腳本中定義過,所以說明字符串長度仍然為零
then
|
11.4.3 文件比較
- test命令的文件比較功能
比較 | 描述 |
---|---|
-d file | 檢查file是否存在並是一個目錄 |
-e file | 檢查file是否存在 |
-f file | 檢查file是否存在並是一個文件 |
-r file | 檢查file是否存在並可讀 |
-s file | 檢查file是否存在並非空 |
-w file | 檢查file是否存在並可寫 |
-x file | 檢查file是否存在並可執行 |
-o file | 檢查file是否存在並屬當前用戶所有 |
-G file | 檢查file是否存在並且默認組與當前用戶相同 |
file1 -nt file2 | 檢查file是否比file2新 |
file1 -ot file2 | 檢查file是否比file2舊 |
11.5 復合條件測試
- fi-then語句允許使用布爾邏輯來組合測試
- [ condition1 ] && [ condition2 ]
- [ condition1 ] || [ condition2 ]
11.6 if-then的高級特性
- 用於數學表達式的雙尖括號
- 用於高級字符串處理功能的雙方括號
11.6.1 使用雙尖括號
- 雙腳括號允許將高級數學表達式放入比較中
- 格式:(( expression ))
- expression:可以是任意的數學賦值或比較表達式
- 如下常用的表達式
符號 | 描述 |
---|---|
val++ | 后增 |
val– | 后減 |
++val | 先增 |
–val | 先減 |
** | 冪運算 |
<< | 左位移 |
& | 位布爾和 |
&& | 邏輯和 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
$cat test
#!/bin/bash
val1=10
if (( $val1 ** 2 > 90 )) //大於號無需轉義
then
(( val2 = $val1 ** 2 ))
echo "The square of $val1 is $val2"
fi
$
$./test
The square of 10 is 100
$
|
11.6.2 使用雙方括號
- 雙方括號命令提供了針對字符串的高級特性
- 格式:[[ expression ]]
- expression:使用了test命令中采用的標准字符串進行比較
- 它提供了test命令為提供的另一個特性——模式匹配
- 模式匹配中,可以定義一個規則表達式來匹配字符串
1
2
3
4
5
6
7
8
9
10
11
12
13
|
$cat test
#!/bin/bash
if [[ $USER == r* ]]
then
echo "Hello $USER"
else
echo "Sorry, I do not know you"
fi
$
$./test
Hello rich
$
|
11.7 case命令
- 格式如下
- case命令會將指定的變量同不同模式進行比較,匹配則shell會執行該模式指定的命令
- 可以通過豎線操作符來分隔模式,在一行輸出多個模式
- 星號會補貨所有跟所有列出的模式都不匹配的值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
//格式:
case variable in
pattern1 | pattern2) commands1;;
pattern3) commands2;;
*) default commands;;
esac
//例程:
$ cat test
#!/bin/bash
case $USER in
rich | barbara)
echo "Welcome, $USER"
echo "Please enjoy your visit";;
testing)
echo "Special testing account";;
jessica)
echo "Do not forget to log off when you're done";;
*)
echo "Sorry, you are not allowed here";;
esac
$
$./test
Welcome, rich
Please enjoy your visit
$
|