六、shell腳本的條件測驗與比較
(一)條件運算式的常見語法
1、條件運算式6種寫法(if,while)
語法1:test<測驗運算式>
語法2:[ <測驗運算式>] #中括號兩端必須要有空格
語法3:[[<測驗運算式>]] #兩端必須要有空格
語法4:((測驗運算式)) #兩端必不需要空格
語法5:(命令運算式)
語法6:命令運算式
實際應用展示
①[]條件運算式
[root@centos6-kvm3 scripts]# [ -e /etc/hosts ] && echo 0 || echo 1
0
[root@centos6-kvm3 scripts]# [ -e /etc/host1 ] && echo 0 || echo 1
1
②test條件運算式:test和[]功能相同
[root@centos6-kvm3 scripts]# test -e /etc/host1 && echo 0 || echo 1
1
[root@centos6-kvm3 scripts]# test -e /etc/hosts && echo 0 || echo 1
0
[root@centos6-kvm3 scripts]# # [] == test
③[[]]雙中括號條件運算式,兩邊需要空格
[root@centos6-kvm3 scripts]# [[ -e /etc/host1 ]] && echo 0 || echo 1
1
[root@centos6-kvm3 scripts]# [[ -e /etc/hosts ]] && echo 0 || echo 1
0
④雙括號條件運算式,兩邊不需要空格
[root@centos6-kvm3 scripts]# (( -e /etc/hosts )) && echo 0 || echo 1
-bash: ((: -e /etc/hosts : division by 0 (error token is "/hosts ")
1
⑤雙括號一般用于計算:
[root@centos6-kvm3 scripts]# ((3>5)) && echo 0 || echo 1
1
[root@centos6-kvm3 scripts]# ((3<5)) && echo 0 || echo 1
0
[root@centos6-kvm3 scripts]# #(())用于計算
⑥expr 運算式:使用括號
[root@centos6-kvm3 scripts]# (expr 1 + 2 &>/dev/null) && echo 0 || echo 1
0
[root@centos6-kvm3 scripts]# (expr 1 + 2 &>/dev/null) && echo 0 || echo 1
0
[root@centos6-kvm3 scripts]# (expr 1 + a &>/dev/null) && echo 0 || echo 1
1
⑦expr 運算式:使用反引號
[root@centos6-kvm3 scripts]# `expr 1 + a &>/dev/null` && echo 0 || echo 1
1
[root@centos6-kvm3 scripts]# `expr 1 + 2 &>/dev/null` && echo 0 || echo 1
0
[root@centos6-kvm3 scripts]#
(二)條件運算式的編輯語法:
1、[<測驗運算式>] && 命令1 ||命令2
如果前面運算式成功,那么執行命令1,否則執行命令2
if [ <測驗運算式>]
then
命令1
else
命令2
fi
2、多命令情況
當命令很多的時候,我們可以使用大括號把所有命令括起來,如下:
[ <測驗運算式> ] && {
命令1
命令2
}||{
命令3
命令4
}
3、只保留執行成功的
[ <測驗運算式>] &&{
命令1
命令2
命令3
}
4、只保留執行失敗的
[<測驗運算式>] || {
命令1
命令2
命令3
}
(三)檔案測驗運算式
man test
-d:檔案為目錄且存在
[root@centos6-kvm3 scripts]# [ -d /etc/hosts ] && echo 0 || echo 1
1
[root@centos6-kvm3 scripts]# [ -d /etc ] && echo 0 || echo 1
0
-f:判斷為檔案且存在
[root@centos6-kvm3 scripts]# [ -f /etc/hosts ] && echo 0 || echo 1
0
-e:判斷存在,為目錄或者檔案
[root@centos6-kvm3 scripts]# [ -e /etc/hosts ] && echo 0 || echo 1
0
[root@centos6-kvm3 scripts]# [ -e /etc ] && echo 0 || echo 1
0
-r:判斷檔案為可讀:
[root@centos6-kvm3 scripts]# [ -r /etc/hosts ] && echo 0 || echo 1
0
[root@centos6-kvm3 scripts]# ll /etc/hosts
-rw-r--r--. 2 root root 352 Nov 19 2018 /etc/hosts
-x:判斷檔案為可執行:
[root@centos6-kvm3 scripts]# [ -x /etc/hosts ] && echo 0 || echo 1
1
[root@centos6-kvm3 scripts]# chmod +x /etc/hosts
[root@centos6-kvm3 scripts]# [ -x /etc/hosts ] && echo 0 || echo 1
0
-s:判斷檔案大小不為0:
[root@centos6-kvm3 scripts]# [ -s /etc/hosts ] && echo 0 || echo 1
0
[root@centos6-kvm3 scripts]# touch oldboy.log
[root@centos6-kvm3 scripts]# [ -s oldboy.log ] && echo 0 || echo 1
1
應用示例:crond
[root@centos6-kvm3 scripts]# cat /etc/init.d/crond
(四)字串測驗運算式的常見功能說明
n:not zero ,[-n "字串" ] 字串長度不為0,運算式為真,
z:zero,[-z "字串" ] 字串長度為0,運算式為真,
["字串1"==“字串2”] 兩個字串相同為真,
[“字串1”!=“字串2”] 兩個字串不相同為真,
注意:
1、字串就用雙引號,
2、等號可以用一個或者兩個,
3、等號兩端必須要有空格,
[root@centos6-kvm3 scripts]# [ -n "oldboy" ] && echo 1 || echo 0
1
[root@centos6-kvm3 scripts]# [ -z "oldboy" ] && echo 1 || echo 0
0
[root@centos6-kvm3 scripts]# char="oldboy"
[root@centos6-kvm3 scripts]# [ -n "$char" ] && echo 1 || echo 0
1
[root@centos6-kvm3 scripts]# unset char
[root@centos6-kvm3 scripts]# [ -n "$char" ] && echo 1 || echo 0
0
[root@centos6-kvm3 scripts]# [ "dd"=="ff" ] && echo 1 || echo 0
1
[root@centos6-kvm3 scripts]# [ "dd" == "ff" ] && echo 1 || echo 0
0
[root@centos6-kvm3 scripts]# [ "dd" == "dd" ] && echo 1 || echo 0
1
[root@centos6-kvm3 scripts]# [ "dd" != "ff" ] && echo 1 || echo 0
1
[root@centos6-kvm3 scripts]# [ "dd" != "dd" ] && echo 1 || echo 0
0
實體應用:
cat /etc/init.d/crond
cat /etc/init.d/network
實體:
[root@centos6-kvm3 scripts]# cat select1.sh
#!/bin/bash
cat << EOF
1.install lamp
2.install lnmp
3.exit
EOF
read -p "請輸入一個序號:" num
[ -z "$num" ] && exit 1 #判斷內容是否為空
expr 2 + $num &>/dev/null
if [ $? -ne 0 ]
then
echo "usage:$0{1|2|3}"
exit 1
fi
if [ $num -eq 1 ]
then
echo "install lamp..."
elif [ $num -eq 2 ]
then
echo "install lnmp ..."
elif [ $num -eq 3 ]
then
echo "bye..."
exit
else
echo "usage:$0{1|2|3}"
exit 1
fi
(五)整數測驗運算式
| 在[]及test中使用的比較運算式 | 在(())和[[]]中使用的比較符號 | 說明 |
|---|---|---|
| -eq | ==或者= | 等于equal |
| -ne | != | 不等于not equal |
| -gt | > | 大于greater then |
| -ge | >= | 大于等于greater equal |
| -lt | < | 小于 less then |
| -le | <= | 小于等于 less equal |
實體
[root@centos6-kvm3 ~]# [ 2 -eq 3 ] && echo 0 || echo 1
1
[root@centos6-kvm3 ~]# [ 2 -gt 3 ] && echo 0 || echo 1
1
[root@centos6-kvm3 ~]# [ 2 -lt 3 ] && echo 0 || echo 1
0
[root@centos6-kvm3 ~]# [ 2 > 3 ] && echo 0 || echo 1
0
[root@centos6-kvm3 ~]# [ 2 \> 3 ] && echo 0 || echo 1
1
[root@centos6-kvm3 ~]# [[ 2 > 3 ]] && echo 0 || echo 1
1
[root@centos6-kvm3 ~]# [[ 2 -gt 3 ]] && echo 0 || echo 1
1
[root@centos6-kvm3 ~]# [[ 2 -lt 3 ]] && echo 0 || echo 1
0
[root@centos6-kvm3 ~]# (( 2 -lt 3 )) && echo 0 || echo 1
-bash: ((: 2 -lt 3 : syntax error in expression (error token is "3 ")
1
[root@centos6-kvm3 ~]# (( 2 > 3 )) && echo 0 || echo 1
1
[root@centos6-kvm3 ~]# (( 2 < 3 )) && echo 0 || echo 1
0
[root@centos6-kvm3 ~]# test 2 -gt 3 && echo 0 || echo 1
1
[root@centos6-kvm3 ~]# test 2 -lt 3 && echo 0 || echo 1
0
總結:
1、雙中括號中使用 字母運算式,
2、雙括號中不適合字母運算式,只適合符號運算式,
3、test運算式只適合符號運算式,
(六)測驗題:使用read的互動方式,來比較兩個整數的大小,
[root@centos6-kvm3 scripts]# cat test3.sh
#!/bin/bash
read -p "請輸入兩個整數:" a b
[ -z "$b" ] && {
echo "請輸入兩個整數,"
exit 1
}
expr $a + $b + 1 &>/dev/null
[ $? -ne 0 ] && {
echo "請輸入兩個整數,"
exit 2
}
[ $a -lt $b ] && {
echo "$a小于$b."
exit 0
}
[ $a -gt $b ] && {
echo "$a大于$b."
exit 0
}
[ $a -eq $b ] && {
echo "$a等于$b."
exit 0
}
================
使用if陳述句:
[root@centos6-kvm3 scripts]# cat test4.sh
#!/bin/bash
read -p "請輸入兩個整數:" a b
[ -z "$b" ] && {
echo "請輸入兩個整數,"
exit 1
}
expr $a + $b + 1 &>/dev/null
[ $? -ne 0 ] && {
echo "請輸入兩個整數,"
exit 2
}
if [ $a -lt $b ]
then
echo "$a小于$b."
elif [ $a -gt $b ]
then
echo "$a大于$b."
else
echo "$a等于$b."
fi
[root@centos6-kvm3 scripts]#
(七)邏輯測驗運算式
| 在[]和test中使用運算子 | 在[[]]和(())中使用運算子 | 說明 |
|---|---|---|
| -a | && | and 與 |
| -o | || | or 或 |
| ! | ! | not 非 |
實體:
[root@centos6-kvm3 scripts]# [ 1 -eq 1 -a -f /etc/hosts ] && echo 1 || echo 0
1
[root@centos6-kvm3 scripts]# [ 1 -eq 2 -a -f /etc/hosts ] && echo 1 || echo 0
0
[root@centos6-kvm3 scripts]# [ 1 -eq 2 -o -f /etc/hosts ] && echo 1 || echo 0
1
[root@centos6-kvm3 scripts]# [ 1 -eq 2 ] -o [ -f /etc/hosts ] && echo 1 || echo 0
-bash: [: too many arguments
0
[root@centos6-kvm3 scripts]# [ 1 -eq 2 ] || [ -f /etc/hosts ] && echo 1 || echo 0
1
[root@centos6-kvm3 scripts]# [ 1 -eq 2 ] && [ -f /etc/hosts ] && echo 1 || echo 0
0
[root@centos6-kvm3 scripts]# [[ 1 -eq 2 || -f /etc/hosts ]] && echo 1 || echo 0
1
[root@centos6-kvm3 scripts]# [[ 1 -eq 2 && -f /etc/hosts ]] && echo 1 || echo 0
0
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/91653.html
標籤:Linux
