1. 程式人生 > >if 條件判斷 和 判斷總結---shell腳本

if 條件判斷 和 判斷總結---shell腳本

選項 但是 語言 echo ng2 other 朋友 系統 solar

本文主要介紹了Shell腳本IF條件判斷和判斷條件總結,本文先是給出了IF條件判斷的語法,然後給出了常用的判斷條件總結,需要的朋友可以參考下。

前言:
      無論什麽編程語言都離不開條件判斷。SHELL也不例外。


      if list then 
          do something here 
      elif list then 
          do another thing here 
      else 
         do something else here 
      fi  
   
EX1:

#!/bin/sh
SYSTEM=`uname
-s` #獲取操作系統類型,我本地是linux if [ $SYSTEM = "Linux" ] ; then #如果是linux的話打印linux字符串 echo "Linux" elif [ $SYSTEM = "FreeBSD" ] ; then echo "FreeBSD" elif [ $SYSTEM = "Solaris" ] ; then echo "Solaris" else echo "What?" fi #ifend

基本上和其他腳本語言一樣。沒有太大區別。不過值得註意的是。[]裏面的條件判斷。

1、字符串判斷

str1 = str2      當兩個串有相同內容、長度時為真
str1 != str2      當串str1和str2不等時為真
-n str1        當串的長度大於0時為真(串非空)
-z str1        當串的長度為0時為真(空串)
str1         當串str1為非空時為真

2、數字的判斷

int1 -eq int2    兩數相等為真
int1 -ne int2    兩數不等為真
int1 -gt int2    int1大於int2為真
int1 -ge int2    int1大於等於int2為真
int1 -lt int2    int1小於int2為真
int1 -le int2    int1小於等於int2為真

3、文件的判斷

-r file     用戶可讀為真
-w file     用戶可寫為真
-x file     用戶可執行為真
-f file     文件為正規文件為真
-d file     文件為目錄為真
-c file     文件為字符特殊文件為真
-b file     文件為塊特殊文件為真
-s file     文件大小非0時為真
-t file     當文件描述符(默認為1)指定的設備為終端時為真

4、復雜邏輯判斷

-a         與
-o        或
!        非

結尾

語法雖然簡單,但是在SHELL裏使用的時候,他的功能變得強大了。

=====================================================================

附 表:

[ -a FILE ] 如果 FILE 存在則為真。
[ -b FILE ] 如果 FILE 存在且是一個塊特殊文件則為真。
[ -c FILE ] 如果 FILE 存在且是一個字特殊文件則為真。
[ -d FILE ] 如果 FILE 存在且是一個目錄則為真。
[ -e FILE ] 如果 FILE 存在則為真。
[ -f FILE ] 如果 FILE 存在且是一個普通文件則為真。
[ -g FILE ] 如果 FILE 存在且已經設置了SGID則為真。 [ -h FILE ] 如果 FILE 存在且是一個符號連接則為真。
[ -k FILE ] 如果 FILE 存在且已經設置了粘制位則為真。
[ -p FILE ] 如果 FILE 存在且是一個名字管道(F如果O)則為真。
[ -r FILE ] 如果 FILE 存在且是可讀的則為真。
[ -s FILE ] 如果 FILE 存在且大小不為0則為真。
[ -t FD ] 如果文件描述符 FD 打開且指向一個終端則為真。
[ -u FILE ] 如果 FILE 存在且設置了SUID (set user ID)則為真。
[ -w FILE ] 如果 FILE 如果 FILE 存在且是可寫的則為真。
[ -x FILE ] 如果 FILE 存在且是可執行的則為真。
[ -O FILE ] 如果 FILE 存在且屬有效用戶ID則為真。
[ -G FILE ] 如果 FILE 存在且屬有效用戶組則為真。
[ -L FILE ] 如果 FILE 存在且是一個符號連接則為真。
[ -N FILE ] 如果 FILE 存在 and has been mod如果ied since it was last read則為真。
[ -S FILE ] 如果 FILE 存在且是一個套接字則為真。
[ FILE1 -nt FILE2 ] 如果 FILE1 has been changed more recently than FILE2, or 如果 FILE1 exists and FILE2 does not則為真。
[ FILE1 -ot FILE2 ] 如果 FILE1 比 FILE2 要老, 或者 FILE2 存在且 FILE1 不存在則為真。
[ FILE1 -ef FILE2 ] 如果 FILE1 和 FILE2 指向相同的設備和節點號則為真。
[ -o OPTIONNAME ] 如果 shell選項 “OPTIONNAME” 開啟則為真。
[ -z STRING ] “STRING” 的長度為零則為真。
[ -n STRING ] or [ STRING ] “STRING” 的長度為非零 non-zero則為真。
[ STRING1 == STRING2 ] 如果2個字符串相同。 “=” may be used instead of “==” for strict POSIX compliance則為真。
[ STRING1 != STRING2 ] 如果字符串不相等則為真。
[ STRING1 < STRING2 ] 如果 “STRING1” sorts before “STRING2” lexicographically in the current locale則為真。
[ STRING1 > STRING2 ] 如果 “STRING1” sorts after “STRING2” lexicographically in the current locale則為真。
[ ARG1 OP ARG2 ] “OP” is one of -eq, -ne, -lt, -le, -gt or -ge. These arithmetic binary operators return true if “ARG1” is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to “ARG2”, respectively. “ARG1” and “ARG2” are integers.

if 條件判斷 和 判斷總結---shell腳本