1. 程式人生 > 其它 >shell 指令碼之條件測試

shell 指令碼之條件測試

Shell 程式設計之條件語句

目錄

一 ,條件測試



1.1 檔案測試與整數測試

1.1.1 test 命令

測試表達式是否成立,若成立,則返回0,否則返回其他數值(返回值使用 echo $? 檢視)

格式1: test 條件表示式

格式2: [ 條件表示式 ] *#條件表示式與中括號兩邊至少各有一個空格



1.1.2 檔案測試

[ 操作符 檔案或目錄 ]

注意: 中括號兩邊要有空格

檔案運算子 釋義 示例
-e filename 如果filename 存在,則為真 [ -e /etc/passwd]
-f filename 如果filename 存在,且為目錄,則為真 [ -d /opt ]
-d filename 如果filename 存在 ,且為檔案,則為真 [ -f /etc/passwd ]
-L filename 如果fiename 存在,且為軟連線,則為真 [ -L /bin ]
-r filename 如果filename存在,且當前使用者可讀,則為真 [ -r /etc/passwd ]
-w filename 如果filename存在,且當前使用者可寫,則為真 [ -w /etc/passwd ]
-x filename 如果filename 存在,且當前使用者可執行,則為真 [ -x /usr/bin/umake ]
-s filename 如果filename存在,且不為空檔案,則為真 [ -s /etc/passwd ]
-S filename 如果filename 存在,且為空,則為真 [ -S /etc/passwd ]
filename1 -nt filename2 如果 filename 1 比 filename 2 新,則為真 [ /etc/passwd -nt /etc/group ]
filename1 -ot filename2 如果 filename1 比 filename2 舊, 則為真 [ /etc/passwd -ot /etc/group ]





1.1.3 整數值比較

[ 整數1 操作符 整數2 ]

測試符 釋義
-eq 等於(Equal)
-ne 不等於(Not Equal)
-gt 大於(Greater Than)
-lt 小於(Lesser Than)
-le 小於或等於(Lesser or Equal)
-ge 大於或等於(Greater or Equal)



1.2 字串測試與邏輯測試



1.2.1 字串比較

格式1: [ 字串1 = 字串2 ] #字串一樣為真

​ [ 字串1 != 字串2 ] #字串不一樣為真

格式2:[ -z 字串 ] #字串為空,則為真

​ [ -n 字串 ] #字串不為空,則為真

操作符 釋義
= 測試字串是否一樣,一樣為真
!= 測試字串是否不一樣,不一樣為真
-z 測試字串是否為空,為空則為真
-n 測試字串是否不為空,不為空則為真
[root@host103 test]# a=abc
[root@host103 test]# b=def
[root@host103 test]# c=""
[root@host103 test]# [ $a = $b ]
[root@host103 test]# echo $?
1
[root@host103 test]# [ $a != $b ]
[root@host103 test]# echo $?
0
[root@host103 test]# [ -z $a ]
[root@host103 test]# echo $?
1
[root@host103 test]# [ -n $a ]
[root@host103 test]# echo $?
0
[root@host103 test]# 



1.2.2 邏輯測試

格式: [ 表示式1 ] 操作符 [ 表示式2 ] ....

格式2: 命令1 操作符 命令2

操作符 釋義
-a 或者 && 邏輯與,“而且”的意思
-o 或者 || 邏輯或,“或者” 的意思
邏輯否(邏輯非)

&& ,邏輯與,前面的條件都執行成功,才只執行後面的



|| ,邏輯或,多個條件任意一個成功即可執行後面的



二,if語句



2.1 if 單分支語句

格式:

if  條件測試
  then   命令序列
fi
[root@host103 ~]# vim abc.sh
#!/bin/bash
read -p "input hi: " args
if [ $args == hi ];then      #如果輸入的字元為“hi ”
    echo "hello"             #則列印“hello”
fi
:wq



2.2 if 多分支語句

格式:

if  條件判斷
    then   命令序列1
    else   命令序列2
fi
[root@host103 ~]# vim abc.sh
#!/bin/bash
read -p "input hi: " args
if [ $args == hi ];then
    echo "hello"
else
    echo "please input hi"
fi
:wq   



2.3 if 多分支語句

格式:

if 條件測試
   then 命令序列1
elif  條件測試2
   then  命令序列2
elif  條件測試3
    then 命令序列3
  ......
else
   命令序列4
fi
[root@host103 ~]# vim abc.sh
#!/bin/bash
read -p "input your score :" num
if [ $num -ge 90 ];then
    echo "great"
elif [ $num -ge 80 ] ;then
    echo "good"
elif [ $num -ge 70 ] ;then
    echo "not bad"
elif [ $num -ge 60 ] ;then
    echo "pass"
else
    echo "fail"
fi
:wq



三,case語句

case 可以用來判斷一個變數的不同取值

格式:

case  $變數  in
值1)
	命令序列1 ;;
值2)
  命令序列2 ;;
*)
  命令序列3 ;;
esac
  
[root@host103 ~]# vim test.sh
#!/bin/bash
read -p "Do you want to know the meaning of life: " args
case $args in
yes)
    echo "you choise yes" ;;
no)
    echo "you choise no" ;;
*)                                         #case 值沒有匹配,則執行此項
    echo "please input /yes/no";;
esac