天天看点

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#