一、使用if—then語句
if--then語句的格式如下:
if command
then
commands
fi
例子:
[root@ceph01 test]# cat if-then.sh
#!/bin/bash
# testing multiple commands in the then section
testuser=root
if grep $testuser /etc/passwd
then
echo The bash files for user $testuser are:
ls -a /$testuser/.b*
fi
運行腳本
[root@ceph01 test]# ./if-then.sh
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
The bash files for user root are:
/root/.bash_history /root/.bash_profile
/root/.bash_logout /root/.bashrc
二、if—then—else語句
if-then-else語句的命令格式:
if command
then
command
else
command
fi
例子:
[root@ceph01 test]# cat if-then-else.sh
#!/bin/bash
# testing the else section
testuser=badtest
if grep $testuser /etc/passwd
then
echo The files for user $testuser are:
ls -a /$testuser/.b*
else
echo "The user name $testuser doesn't exist on this system"
fi
運行腳本:
[root@ceph01 test]# ./if-then-else.sh
The user name badtest doesn't exist on this system
三、嵌套if語句
有時需要在腳本代碼中檢查幾種情況,這時可以不必編寫單獨的if-then語句,可以使用else部分的另一版本,稱爲elif
elif以另一個if-then語句繼續else部分:
if command1
then
commends
elif command2
then
more commands
fi
也可以把多個elif語句串在一起,創建一個大的if-then-elif組:
if command1
then
command set 1
elif command2
then
command set 2
elif command3
then
command set 3
elif command4
then
command set 4
fi
四、test命令
test命令提供一種檢測if-then語句中不同條件的的方法。如果test命令中列出的條件評估值爲true,test命令以0退出狀態代碼退出,這使if-then語句使用與其他編程語言中的if-then語句一樣的方式運行。如果條件爲false,則test命令退出,使得if-then語句失敗。
test命令的格式:
test condition
if test condition
then
commands
fi
test命令提供一種檢測if-then語句,test評估以下三類:數值比較;字符串比較;文件比較。
1>數值比較:
[root@ceph01 test]# cat test-value.sh
#!/bin/bash
#using numeric test comparisons
val1=10
val2=11
if [ $val1 -gt 5 ] #測試val1是否大於5
then
echo "The test value $val1 is greater than 5"
fi
if [ $val1 -eq $val2 ] #測試val1和val2是否相等
then
echo "The values are equal"
else
echo "The values are different"
fi
運行腳本:
[root@ceph01 test]# ./test-value.sh
The test value 10 is greater than 5
The values are different
2>字符串比較:
字符串相等
[root@ceph01 test]# cat test-string.sh
#!/bin/bash
#testing string equality
testuser=root
if [ $USER != $testuser ]
then
echo "This isn't $testuser"
else
echo "Welcome $testuser"
fi
運行腳本:
[root@ceph01 test]# ./test-string.sh
Welcome root
字符串順序:
大於和小宇符號一定要轉義,否則shell會當做重定向符號;
大於和小於順序與在sort命令中的順序不同
[root@ceph01 test]# cat test-string-order.sh
#!/bin/bash
#mis-using string comparisons
val1=bashball
val2=hockey
if [ $val1 > $val2 ]
then
echo "$val1 is greater than $val2"
else
echo "$val1 is less than $val2"
fi
運行腳本:
[root@ceph01 test]# ./test-string-order.sh
bashball is greater than hockey
字符串大小
[root@ceph01 test]# cat test-string-length.sh
#!/bin/bash
#testing string length
val1=testing
val2=''
if [ -n $val1 ]
then
echo "Test string '$val1' is not empty"
else
echo "The string '$val1' is empty"
fi
if [ -z $val2 ]
then
echo "The string '$val2' is empty"
else
echo "The string '$val2' is not empty"
fi
if [ -z $val3 ]
then
echo "The string '$val3' is empty"
else
echo "The string '$val3' is not empty"
fi
運行腳本:
[root@ceph01 test]# ./test-string-length.sh
Test string 'testing' is not empty
The string '' is empty
The string '' is empty
3>文件比較
檢查目錄
[root@ceph01 test]# cat test-file-leap.sh
#!/bin/bash
# look before you leap
if [ -d $HOME ]
then
echo "Your HOME directory exists"
cd $HOME
ls -a
else
echo "There's a problem with your HOME directory"
fi
運行腳本:
[root@ceph01 test]# ./test-file-leap.sh
Your HOME directory exists
. .cshrc
.. fio-ceph.sh
anaconda-ks.cfg force-eth0-100Mbps.sh
.ansible lvm-resize-sda.sh
.bash_history .pki
.bash_logout .ssh
.bash_profile .tcshrc
.bashrc updates
.cache .viminfo
CentOS-7-aarch64-Everything.iso .Xauthority
.config
檢查對象是否存在
[root@ceph01 test]# cat test-file-checking.sh
#!/bin/bash
# checking if a directory exists
if [ -e $HOME ]
then
echo "OK on the directory,now let's check the file"
#checking if a file exists
if [ -e $HOME/testing ]
then
#the file exists,append data to it
echo "Appending date to existing file"
date >> $HOME/testing
else
#the file doesn't exist,create a new file
echo "Creating new file"
date > $HOME/testing
fi
else
echo "Sorry,you don't have a HOME directory"
fi
運行腳本:
[root@ceph01 test]# ./test-file-checking.sh
OK on the directory,now let's check the file
Creating new file
[root@ceph01 test]# ./test-file-checking.sh
OK on the directory,now let's check the file
Appending date to existing file
檢查文件
[root@ceph01 test]# cat test-file-check.sh
#!/bin/bash
# check if a file
if [ -e $HOME ]
then
echo "The object exists,is it a file"
if [ -f $HOME ]
then
echo "Yes,it's a file!"
else
echo "No,it's not a file!"
if [ -f $HOME/.bash_history ]
then
echo "But this is a file!"
fi
fi
else
echo "Sorry,the object doesn't exist"
fi
運行腳本:
[root@ceph01 test]# ./test-file-check.sh
The object exists,is it a file
No,it's not a file!
But this is a file!
是否能讀
[root@ceph01 test]# cat test-file-read.sh
#!/bin/bash
#testing if you can read a file
pwfile=/etc/shadow
#first,test if the file exists,and is a file
if [ -f $pwfile ]
then
#new test if you can read it
if [ -r $pwfile ]
then
tail $pwfile
else
echo "Sorry,I'm unable to read the $pwfile file"
fi
else
echo "Sorry,the file $file doesn't exist"
fi
運行腳本:
[root@ceph01 test]# ./test-file-read.sh
gnome-initial-setup:!!:17530::::::
rpcuser:!!:17530::::::
nfsnobody:!!:17530::::::
avahi:!!:17530::::::
postfix:!!:17530::::::
sshd:!!:17530::::::
ntp:!!:17530::::::
oprofile:!!:17530::::::
tcpdump:!!:17530::::::
ceph:!!:17885::::::
檢查空文件
[root@ceph01 test]# cat test-file-null.sh
#!/bin/bash
#testing if a file is empty
file=t15test
touch $file
if [ -s $file ]
then
echo "The $file file exists and has data in it"
else
echo "The $file exists and is empty"
fi
date > $file
if [ -s $file ]
then
echo "The $file file has data in it "
else
echo "The $file is still empty"
fi
運行腳本
[root@ceph01 test]# ./test-file-null.sh
The t15test exists and is empty
The t15test file has data in it
檢查是否能夠向文件中寫入數據
-w比較確定是否有權能夠向文件中寫入數據
[root@ceph01 test]# cat test-file-check-write.sh
#!/bin/bash
# checking if a file is writeable
logfile=$HOME/t16test
touch $logfile
chmod u-w $logfile
now='date +%Y%m%d-%H%M'
if [ -w $logfile ]
then
echo "The program ran at: $now" > $logfile
echo "The first attempt succeeded"
else
echo "The first attempt failed!"
fi
chmod u+w $logfile
if [ -w $logfile ]
then
echo "The program ran at: $now" > $logfile
echo "The second attempt succeeded"
else
echo "The second attempt failed"
fi
運行腳本:
[root@ceph01 test]# ./test-file-check-write.sh
The first attempt succeeded
The second attempt succeeded
檢查是否能夠運行文件
-x比較你是否有指定文件的運行權限
[root@ceph01 test]# cat test-file-check-run.sh
#!/bin/bash
# testing file execution
if [ -x test-file-checking.sh ]
then
echo "You can run the script:"
./test-file-checking.sh
else
echo "Sorry,you are unable to execute the script"
fi
運行腳本:
[root@ceph01 test]# ./test-file-check-run.sh
You can run the script:
OK on the directory,now let's check the file
Appending date to existing file
檢查所有權
-o比較可以容易地檢查你是否是文件的所有者:
[root@ceph01 test]# cat test-file-check-ownser.sh
#!/bin/bash
# check file ownsership
if [ -o /etc/passwd ]
then
echo "You're the owner of the /etc/passwd file"
else
echo "Sorry,you're not the owner of the /etc/passwd file"
fi
運行腳本:
[root@ceph01 test]# ./test-file-check-ownser.sh
Sorry,you're not the owner of the /etc/passwd file
檢查文件日期
-nt比較確定一個文件是否比另一個文件新,如果一個文件更新,他就是一個更近的文件創建時間。-ot比較確定一個文件是否比另一個文件更舊,如果文件更舊,它會有一個比較久遠的創建時間:
[root@ceph01 test]# cat test-file-check-date.sh
#!/bin/bash
# testing file dates
if [ ./outfile -nt ./test-file-check.sh ]
then
echo "The outfile file is newer than test-file-check.sh"
else
echo "The test-file-check.sh file is newer than outfile"
fi
if [ ./outfile -ot ./test-fiel-check.sh ]
then
echo "The outfile file is older than the test-file-check.sh"
fi
運行腳本:
[root@ceph01 test]# ./test-file-check-date.sh
The test-file-check.sh file is newer than outfile
五、複合條件檢查(布爾操作符:&& ||)
if-then語句可以使用布爾邏輯來合併檢查條件,可以使用兩個布爾操作符:
[ condition ] && [ condition2 ]
[ condition ] || [ condition2 ]
&&:AND布爾操作符(兩者都成立纔會執行then)
|| :OR布爾操作符(兩者滿足其一就會執行then)
[root@ceph01 test]# cat bool.sh
#!/bin/bash
# testing compound comparisons
if [ -d $HOME ] && [ -w $HOME/testing ]
then
echo "The file exists and you can write to it"
else
echo "I can't write to the file"
fi
運行腳本:
[root@ceph01 test]# ./bool.sh
The file exists and you can write to it
六、if-then的高級特徵
雙圓括號表示數學表達式;
雙方括號表示高級字符串處理函數。
(1)雙圓括號
雙圓括號命令允許在比較中包含高級數學公式。雙圓括號命令的格式是:
((expression))
[root@ceph01 test]# cat double-parenthesis.sh
#!/bin/bash
# using double parenthesis
val1=10
if (( $val1 ** 2 > 90 ))
then
(( val2 = $val1 ** 2 ))
echo "The square of $val1 is $val2"
fi
運行腳本:
[root@ceph01 test]# ./double-parenthesis.sh
The square of 10 is 100
(2)雙方括號
雙方括號命令爲字符串比較提供高級功能,格式爲:[ [ expression ] ]
[root@ceph01 test]# cat pattern-matching.sh
#!/bin/bash
# using pattern matching
if [[ $USER == r* ]]
then
echo "Hello $USER"
else
echo "Sorry,I don't know you"
fi
運行腳本:
[root@ceph01 test]# ./pattern-matching.sh
Hello root
七、case命令
case命令將指定的變量與不同的模式進行比較。如果變量與模式匹配,shell執行爲該模式指定的命令。
命令格式:
case Varisble in
pattern1 | pattern2) commands1;
pattern3) commands2;;
*) default commands;;
esac
示例:
[root@ceph01 test]# cat case.sh
#!/bin/bash
# using the case command
case $USER in
root | barbara)
echo "Welcome $USER"
echo "Please enjoy your visit";;
testing)
echo "Special testing account";;
jessica)
echo "Don't forget to log off when you're done";;
*)
echo "Sorry,you're not allowed here";;
esac
運行腳本:
[root@ceph01 test]# ./case.sh
Welcome root
Please enjoy your visit