天天看點

shell 實練(5)if 判斷

練習的小内容:
判斷目前檔案夾下是否存在a.txt檔案,若存在則建立a-1.txt檔案;否則建立t.txt檔案;
if 的三種條件表達式
1、
if  條件一;then
      command1
else
     command2
fi

code:
  1 #!/bin/bash
   2 
   3 if  ls|grep a.txt;  then
   4         touch a-1.txt
   5 else    
   6         touch t.txt
   7 fi      Loong:/home/yee/if# ls
 if-1.sh  if-2.sh
 Loong:/home/yee/if# sh -x if-1.sh 
 + ls
 + grep a.txt
 + touch t.txt
 Loong:/home/yee/if# ls
 if-1.sh  if-2.sh  t.txt
 Loong:/home/yee/if# touch a.txt
 Loong:/home/yee/if# ls
 a.txt  if-1.sh  if-2.sh  t.txt
 Loong:/home/yee/if# sh -x if-1.sh 
 + ls
 + grep a.txt
 a.txt
 + touch a-1.txt
 Loong:/home/yee/if# 2、
if [表達式];then                if與【】間要留白格,否則報錯;
command1
fi           
更複雜的情況,則可以使用這個文法:
if [ 條件判斷式一 ]; then
 當條件判斷式一成立時,可以進行的指令工作内容;
elif [ 條件判斷式二 ]; then
 當條件判斷式二成立時,可以進行的指令工作内容;
 else
 當條件判斷式一與二均不成立時,可以進行的指令工作内容;
fi

檔案表達式
條件表達式if [ -f  file]   如果檔案存在
 if [ -d...  ]   如果目錄存在
 if [ -s file ]   如果檔案存在且非空 
 if [ -r file ]   如果檔案存在且可讀 
 ] 
  
  如果檔案存在且可寫
 
 if [ -x file 
  ] 
  
  如果檔案存在且可執行 
  
 
 if [ -e dir||file] 如果指定的檔案或者目錄存在傳回真
 
    [ -z STRING ] 如果STRING的長度為零則為真 
 
    [ -n STRING ] 如果STRING的長度非零則為真 
 
    [ STRING1 = STRING2 ] 如果兩個字元串相同則為真 
 
    [ STRING1 != STRING2 ] 如果字元串不相同則為真
 

 整數變量表達式
 

if [ int1 -eq int2]   如果int1等于int2                       
 -eq  -ne -lt  -nt隻能用于整數,不适用于字元串,字元串等于用指派号=
 
 if [ int1 -ne int2]    如果不等于   
 
if [ int1 -ge int2] 
  
  
  
  
  
 如果>=                                整數條件表達式,大于,小于,shell裡沒有> 和<,會被當作尖括号,隻有-ge,-gt,-le,lt
 if [ int1 -gt int2] 
  
  
  
  
  
 如果>
 if [ int1 -le int2] 
  
  
  
  
  
 如果<=
 if [ int1 -lt int2] 
  
  
  
  
  
 如果<
 
  
  
  
 

  字元串變量表達式
 

If 
  [ $a = $b] 
  
  
  
  
  
  
  
  
  
  
  
  
   
  如果string1等于string2               
 條件表達式引用變量要帶$,
 比較$a和$b,而不是比較a和b
 
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
       字元串允許使用指派号做等号
 
if 
  [ $string1 != 
  $string2] 
  
 如果string1不等于string2 
  
  
  
  
  
  
  =放在别的地方是指派,放在if [ ]裡就是字元串等于, =作為等于時,其兩邊都必須加空格,否則失效
 
if  [ -n$string ]           
 
if 
  [ -z $string 
 ] 
  
  
  
  
  
  
  
  
  
 如果string 為空
 if  [ $sting]                 如果string非空,傳回0 (和-n類似)  
  邏輯非!                  
 if [ ! 表達式 ]
 if [ ! -d $num]         如果不存在目錄$num 

 

  邏輯與–a 
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
                條件表達式的并列
 
 if [ 表達式1 
  –a 
  表達式2 ]
 


  
  
  
 邏輯或-o           
  
  
  
  
  
  
  
  
               條件表達式的或
 
 if [ 表達式1 
  –o 表達式2 ]
 

  
  
 
   
 • 表達式與前面的=
• 邏輯符号就正常的接其他表達式,沒有任何括号( ),就是并列
                if [ -z "$JHHOME" -a -d $HOME/
 

         注意邏輯與-a與邏輯或-o很容易和其他字元串或檔案的運算符号搞混



code:
 1 #!/bin/bash
   2 
   3 filename=/home/yee/if/a.txt
   4 if [ -f $filename ]; then
   5         touch a-1.txt
   6 fi
   7 if [ ! -e $filename ]; then
   8         touch t.txt
   9 fiLoong:/home/yee/if# ll
 總計 12
 -rw-r--r-- 1 root root  75 11-07 11:42 if-1.sh
 -rw-r--r-- 1 root root 130 11-07 16:08 if-2.sh
 -rw-r--r-- 1 root root  38 11-07 14:21 if-3.sh
 Loong:/home/yee/if# sh -x if-2.sh 
 + filename=/home/yee/if/a.txt
 + '[' -f /home/yee/if/a.txt ']'
 + '[' '!' -e /home/yee/if/a.txt ']'
 + touch t.txt
 Loong:/home/yee/if# ll
 總計 12
 -rw-r--r-- 1 root root  75 11-07 11:42 if-1.sh
 -rw-r--r-- 1 root root 130 11-07 16:08 if-2.sh
 -rw-r--r-- 1 root root  38 11-07 14:21 if-3.sh
 -rw-r--r-- 1 root root   0 11-07 16:11 t.txt
 Loong:/home/yee/if# 3、
if test 表達式 ;then
command1
fi
例:
if test $num -eq0     等價于  

   
test 表達式,沒有 [      
1 #!/bin/bash
  2 
  3 filename=/home/yee/if/a.txt
  4 if test -f $filename
  5 then    
  6         touch a-1.txt
  7 else
  8         touch t.txt
  9 fi      
Loong:/home/yee/if# ll
總計 12
-rw-r--r-- 1 root root  75 11-07 11:42 if-1.sh
-rw-r--r-- 1 root root 130 11-07 16:08 if-2.sh
-rw-r--r-- 1 root root  38 11-07 14:21 if-3.sh
-rw-r--r-- 1 root root   0 11-07 16:11 t.txt
Loong:/home/yee/if# sh -x if-3.sh 
+ filename=/home/yee/if/a.txt
+ test -f /home/yee/if/a.txt
+ touch t.txt
Loong:/home/yee/if# ll
總計 12
-rw-r--r-- 1 root root  75 11-07 11:42 if-1.sh
-rw-r--r-- 1 root root 130 11-07 16:08 if-2.sh
-rw-r--r-- 1 root root 103 11-07 16:20 if-3.sh
-rw-r--r-- 1 root root   0 11-07 16:20 t.txt
Loong:/home/yee/if# touch a.txt
Loong:/home/yee/if# sh -x if-3.sh 
+ filename=/home/yee/if/a.txt
+ test -f /home/yee/if/a.txt
+ touch a-1.txt
Loong:/home/yee/if# ll
總計 12
-rw-r--r-- 1 root root   0 11-07 16:21 a-1.txt
-rw-r--r-- 1 root root   0 11-07 16:21 a.txt
-rw-r--r-- 1 root root  75 11-07 11:42 if-1.sh
-rw-r--r-- 1 root root 130 11-07 16:08 if-2.sh
-rw-r--r-- 1 root root 103 11-07 16:20 if-3.sh
-rw-r--r-- 1 root root   0 11-07 16:20 t.txt
Loong:/home/yee/if#