1. 程式人生 > >Shell學習之條件測試(四)

Shell學習之條件測試(四)

string ica 邏輯 install script 數值 || 文件 highlight

Shell學習之條件測試

目錄

邏輯測試

文件測試

數值比較

字符串比較

邏輯測試

格式:

  [ 表達式 ]  操作符 [ 表達式2 ] ……

  命令1  操作符  命令2 ……

常用的操作符 ( 註意:-a和-o放在[]裏面用,&&和||放在[]外面用 )

    -a  或  &&           邏輯與

    -o  或 ||             邏輯或

      !               邏輯否

  


文件測試

文件測試

格式1:  [  操作符 文件或目錄  ]  

格式2:test  操作符 文件或目錄 

常用的測試操作符

    -d :測試是否為目錄( Directory )

    -e :測試目錄或文件是否存在(Exist)

    -f :測試是否為文件(File)

    -r :測試當前用戶是否可讀(read)

    -w:測試當前用戶是否可寫(write)

    -x :測試當前用戶是否可執行(excute)

例子:備份Mysql數據庫,業務代碼沒有完善

#/bin/bash
back_dir=/var/mysql_back

if !test -d $back_dir;then
	mkdir -p $back_dir
fi
echo "開始備份"

  


數值比較

格式1:[ 整數1 操作符 整數2 ]

格式2: test 整數1 操作符 整數2

常用的測試操作符

    -eq : 等於 (Equal)

    -ne : 不等於 (Not Equal)

    -gt : 大於(Greater Than)

    -lt : 小於 (Lesser Than)

    -le : 小於或等於(Lesser or Equal)

    -ge : 大於或等於(Greater or Equal)

  

例子

#/bin/bash
if [ $UID -ne 0];then
	echo "沒有權限"
	exit
fi
yum -y install httpd

  

字符串比較

格式1:[ 字符串1 = 字符串2 ]

     [ 字符串1 != 字符串2 ]

格式2:[ -z 字符串 ]

常用的測試操作符

  = : 字符串內容相同

  != : 字符串內容不同

  -z : 字符串內容為空

  

例子

#/bin/bash
if [ $USER = "root"];then
	yum -y install httpd
fi
	echo "沒有權限"
	exit

  

所有表達式

( EXPRESSION )
              EXPRESSION is true

       ! EXPRESSION
              EXPRESSION is false

       EXPRESSION1 -a EXPRESSION2
              both EXPRESSION1 and EXPRESSION2 are true

       EXPRESSION1 -o EXPRESSION2
              either EXPRESSION1 or EXPRESSION2 is true

       -n STRING
              the length of STRING is nonzero

       STRING equivalent to -n STRING

       -z STRING
              the length of STRING is zero

       STRING1 = STRING2
              the strings are equal

       STRING1 != STRING2
              the strings are not equal

       INTEGER1 -eq INTEGER2
              INTEGER1 is equal to INTEGER2

       INTEGER1 -ge INTEGER2
              INTEGER1 is greater than or equal to INTEGER2

       INTEGER1 -gt INTEGER2
              INTEGER1 is greater than INTEGER2

       INTEGER1 -le INTEGER2
              INTEGER1 is less than or equal to INTEGER2

       INTEGER1 -lt INTEGER2
              INTEGER1 is less than INTEGER2

       INTEGER1 -ne INTEGER2
              INTEGER1 is not equal to INTEGER2

       FILE1 -ef FILE2
              FILE1 and FILE2 have the same device and inode numbers

       FILE1 -nt FILE2
              FILE1 is newer (modification date) than FILE2

       FILE1 -ot FILE2
              FILE1 is older than FILE2

       -b FILE
              FILE exists and is block special

       -c FILE
              FILE exists and is character special

       -d FILE
              FILE exists and is a directory

       -e FILE
              FILE exists

       -f FILE
              FILE exists and is a regular file

       -g FILE
              FILE exists and is set-group-ID

       -G FILE
              FILE exists and is owned by the effective group ID

       -h FILE
              FILE exists and is a symbolic link (same as -L)

       -k FILE
              FILE exists and has its sticky bit set

       -L FILE
              FILE exists and is a symbolic link (same as -h)

       -O FILE
              FILE exists and is owned by the effective user ID

       -p FILE
              FILE exists and is a named pipe

       -r FILE
              FILE exists and read permission is granted

       -s FILE
              FILE exists and has a size greater than zero

       -S FILE
              FILE exists and is a socket

       -t FD  file descriptor FD is opened on a terminal

       -u FILE
              FILE exists and its set-user-ID bit is set

       -w FILE
              FILE exists and write permission is granted

       -x FILE
              FILE exists and execute (or search) permission is granted

  

Shell學習之條件測試(四)