目錄
使用結構化命令
前言
>>>在shell腳本中,有一類命令會根據條件進行判斷,是腳本去決定要執行哪些命令,跳過哪些命令這樣的命令稱爲結構化命令(structured command),包括:if-then和case語句,for、while和until循環
①使用if-then語句
基本語法格式(if與fi成對出現)
if command #如果if語句行的命令的退出狀態碼值爲0,then後面的命令都會被執行
then
commands
fi
或者:
if command;then
commands
fi
>>>例子:
[bei@localhost test]$ cat if.sh
#!/bin/bash
val=10
if [ $val -eq 10 ]
then
echo "val is equal 10"
fi
[bei@localhost test]$ bash if.sh
val is equal 10
>>>如果條件不成立時也要執行命令,可以使用if-then-else語句
[bei@localhost test]$ cat if.sh
#!/bin/bash
val=11
if [ $val -eq 10 ]
then
echo "val is equal 10"
else
echo "val is not equal 10"
fi
[bei@localhost test]$ bash if.sh
val is not equal 10
此時if語句行的命令的退出狀態碼值不爲0,bash shell執行else部分的命令
>>>如果有多個條件需要判斷可以使用 if-elif-else語句
[bei@localhost test]$ cat if.sh
#!/bin/bash
val=11
if [ $val -eq 10 ]
then
echo "val is equal 10"
elif [ $val -gt 10 ]
then
echo "vla is greater than 10"
else
echo "val is less than 10"
fi
[bei@localhost test]$ bash if.sh
vla is greater than 10
此時elif語句行的命令的退出狀態碼值爲0,bash shell執行elif-then部分的命令
>>>如果需要判斷腳本中的多種條件,也可以在then和else後面可以嵌套使用if-then語句
[bei@localhost test]$ cat if.sh
#!/bin/bash
val=11
if [ $val -eq 10 ]
then
echo "val is equal 10"
else
if [ $val -gt 10 ] #else後面嵌套了一個if循環
then
echo "val is greater than 10"
else
echo "val is less than 10"
fi #注意:不能忘記這個fi,因爲if和fi是成對出現的
fi
[bei@localhost test]$ bash if.sh
val is greater than 10
if-then的高級特性:
bash shell有兩個比較常用的擴展,提供了在if-then語句中使用高級特性
>>>用於數學表達式的雙圓括號
>>>用於高級字符串處理功能的雙方括號
雙圓括號:用於數學表達式
>>>語法格式:((expression))
>>>雙圓括號中命令符號彙總
符號 |
描述 |
val++ |
後增 |
val-- |
後減 |
++val |
先增 |
--val |
先減 |
! |
邏輯求反 |
~ |
位求反 |
** |
冪運算 |
<< |
左位移 |
>> |
右位移 |
& |
位布爾與 |
| |
位布爾或 |
&& |
邏輯與 |
|| |
邏輯或 |
舉例:
[bei@localhost test]$ cat if.sh
#!/bin/bash
a=1;b=2
if (( a > 0 && b > 0 ))
then
echo "a和b都大於0"
else
echo "a或b至少一個不大於0"
fi
[bei@localhost test]$ bash if.sh
a和b都大於0
區別:&和&&
>>>10&01 ——>00
>>>10&&01——>1
雙方括號:用於高級字符串處理功能
>>>使用雙方括號實現了模式匹配過程
②case
例子:如果腳本存在多個判斷條件,使用if-then語句麻煩,代碼冗長
[bei@localhost test]$ cat if.sh
#!/bin/bash
USER="administrator"
if [ $USER = "student" ]
then
echo "學生用戶登錄"
elif [ $USER = "teacher" ]
then
echo "教師用戶登錄"
elif [ $USER = "administrator" ]
then
echo "管理員登錄"
else
echo "遊客登錄"
fi
[bei@localhost test]$ bash if.sh
管理員登錄
此時,可以使用case語句匹配多個判斷條件
基本語法結構:
case varible in
pattern1) command1;;
pattern2) command2;;
*) comands;;
esac
例子
[bei@localhost test]$ cat case.sh
#!/bin/bash
USER="administrator"
case $USER in
"student") echo "學生用戶登錄";;
"teacher") echo "教師用戶登錄";;
"administrator") echo "管理員登錄";;
*) echo "遊客登錄";;
esac
[bei@localhost test]$ bash case.sh
管理員登錄
③test命令
>>>在bash shell中有一個命令 test,這個命令提供了if-then語句中測試不同條件的途徑
>>>如果在test命令中列出的條件成立,test命令就會退出並返回退出狀態碼0
>>>如果在test命令中列出的條件不成立,test命令就會退出並返回退出狀態碼1
>>>只有當退出狀態碼爲0時,then後面的代碼部分纔會被執行,否則執行else後面的代碼部分
>>>test命令和if-then結合使用情況:
if test condition
then
commands
fi
>>>但是帶上test使用方式不夠簡化,推薦格式 (左右方括號與condition之間各有一個空格,必須有的)
if [ condition ]
then
commands
fi
test命令判斷的三種類型:
>>>數值比較
>>>字符串比較
>>>文件比較
數值比較(bash shell僅處理整數)
>>>可以測試數字和變量
比較 |
描述 |
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 |
案例:
[bei@localhost test]$ cat test.sh
#!/bin/bash
val=10
if [ $val -eq 0 ]
then
echo "val=0"
elif [ $val -gt 0 ]
then
echo "val>0"
elif [ $val -lt 0 ]
then
echo "val<0"
fi
[bei@localhost test]$ bash test.sh
val>0
字符串比較
比較 |
描述 |
|
str1 = str2 |
判斷str1是否與str2相同 |
|
str1 != str2 |
判斷str1是否與str2不同 |
|
str1 \< str2 |
判斷str1是否比str2小, |
必須進行轉義,按照ASCII比較 而sort與ASCII排序不同, sort命令排序時,小寫字母小於大寫字母 |
str1 \> str2 |
判斷str1是否比str2大 |
必須進行轉義,按照ASCII比較 |
-n “str1” |
判斷str1長度是否不爲0 |
如果不是很確定一個變量的內容 |
-z “str1“ |
判斷str1長度是否爲0 |
|
>>>使用雙方括號:實現模式匹配,字符串裏的通配符 * 才能生效
例子
[bei@localhost test]$ cat if.sh
#!/bin/bash
USER="administrator"
if [[ $USER == a* ]]
then
echo "用戶開頭是a"
else
echo "用戶開頭不是a"
fi
[bei@localhost test]$ bash if.sh
用戶開頭是a
大於號沒有轉義的錯誤案例
[bei@localhost test]$ cat test.sh
#!/bin/bash
val1=a
val2=b
if [ $val1 > $val2 ]
then
echo "val1 is greater than val2"
else
echo "val2 is greater than val1"
fi
[bei@localhost test]$ bash test.sh
val1 is greater than val2
[bei@localhost test]$ ls -al b
-rw-rw-rw-. 1 bei bei 0 Sep 20 19:29 b
>>>此腳本把大於號解釋成了輸出重定向,因此,創建了一個名爲b的文件,由於重定向是成功的,所以test命令返回狀態碼爲0,then後面的命令會被執行,這種用法是錯誤的, <>必須進行轉義
文件比較(重點)
>>>測試Linux系統上文件和目錄的狀態
比較 |
描述 |
-d file |
檢查file是否存在並且是一個目錄 |
-e file |
檢查file是否存在(文件或目錄) |
-f file |
檢查file是否存在並且是一個文件 |
-r file |
檢查file是否存在並且用戶具備可讀權限 |
-w file |
檢查file是否存在並且用戶具備可寫權限 |
-x file |
檢查file是否存在並且用戶具備可執行權限 |
-s file |
檢查file是否存在並且是否爲空 |
-O file |
檢查file的屬主是否是當前執行腳本的用戶 |
-G file |
檢查file的屬組是否是當前執行腳本用戶的基本組(不考慮附加組) |
file1 -ot file2 file1 -nt file2 |
檢查file1與file2文件的新舊 |
案例
>>>檢查對象是否存在,並判斷是目錄還是文件
[bei@localhost test]$ cat test.sh
#!/bin/bash
if [ -e /home ] #判斷對象是否存在
then
echo "/home is exist"
if [ -d /home ] #判斷對象是否爲目錄
then
echo "/home is a directory"
elif [ -f /home ] #判斷對象是否爲文件
then
echo "/home is a file"
fi
else
echo "/home isn't exist"
fi
[bei@localhost test]$ bash test.sh
/home is exist
/home is a directory
>>>檢查file是否存在且爲文件,並判斷是否可讀、可寫、可執行
[bei@localhost test]$ cat test.sh
#!/bin/bash
file=/etc/passwd
if [ -f $file ] #檢查file是否存在且爲文件
then
if [ -r $file ] #檢查file是否可讀
then
echo "You can read $file"
else
echo "You can't read $file"
fi
if [ -w $file ] #檢查file是否可寫
then
echo "You can write $file"
else
echo "You can't write $file"
fi
if [ -x $file ] #檢查file是否可執行
then
echo "You can excute $file"
else
echo "You can't excute $file"
fi
else
echo "the file is not exist"
fi
[bei@localhost test]$ bash test.sh
You can read /etc/passwd
You can't write /etc/passwd
You can't excute /etc/passwd
>>>檢查文件所屬關係和默認屬組關係
[root@localhost test]# cat test.sh
#!/bin/bash
file=/etc/passwd
#使用 -O 測試你是否爲文件的屬主
if [ -O $file ]
then
echo "You are the owner of $file"
else
echo "You aren't the owner of $file"
fi
#使用 -G 檢查你的基本組和用戶的基本組是否匹配
if [ -G $file ]
then
echo "You are in the same group as $file"
else
echo "$file is not owned by your group"
fi
[root@localhost test]# bash test.sh
You are the owner of /etc/passwd
You are in the same group as /etc/passwd
>>>檢查文件日期
[bei@localhost test]$ cat test.sh
#!/bin/bash
#使用-nt比較兩個文件新舊
if [ ./1.txt -nt ./2.txt ]
then
echo "1.txt is newer than 2.txt"
else
echo "1.txt is older than 2.txt"
fi
[bei@localhost test]$ bash test.sh
1.txt is older than 2.txt
>>>使用複合條件測試
有兩種布爾運算符:[ condition1 ] && [ condition2 ] 邏輯與:兩個條件都滿足,才爲真,否則爲假
[ condition1 ] || [ condition2 ] 邏輯或:其中一個條件滿則,爲真,否則爲假
[bei@localhost test]$ cat test.sh
#!/bin/bash
#使用&&邏輯與複合條件測試
if [ -e ./1.txt ] && [ -e ./2.txt ]
then
echo "1.txt和2.txt都存在"
else
echo "至少1.txt或2.txt其中一個不存在"
fi
[bei@localhost test]$ bash test.sh
1.txt和2.txt都存在
說明:
>>>以上內容是本人學習的總結
>>>如還有錯誤,請留言,指正
>>>亦可分享自己的想法,互相學習