shell note

# x=*
# echo $x
hello.sh menus.sh misc.sh phonebook tshift.sh
# echo '$x'
$x
# echo "$x"
*
这个例子可以看出无引号、单引号和双引号之间的区别。在最后一种情况中,双引号告诉shell在引号内照样进行变量名替换,所以shell把$x替换为*,因为双引号中不做文件名替换,所以就把*作为要显示的值传递给echo。 对于第一种情况需要进一步说明,shell在给变量赋值时不进行文件名替换(这从第三种情况中也能看出来),各步骤发生的精确次序如下: shell扫描命令行,把x的值设为星号*;
shell再次扫描命令行,碰到星号*,把它替换成当前目录下的文件清单;
shell启动执行echo命令,把文件清单作为参数传递给echo.
这个赋值的先后次序非常重要:shell先作变量替换,然后作文件名替换,最后把这行处理为参数 
单引号和双引号都能关闭shell对特殊字符的处理。不同的是,双引号没有单引号严格,单引号关闭所有有特殊作用的字符,而双引号只要求shell忽略大多数,具体的说,就是①美元符号②反引号③反斜杠,这3种特殊字符不被忽略。 不忽略美元符号意味着shell在双引号内部也进行变量名替换。
双引号可有可无;单引号主要用在原样输出中。

TWO=2;
echo ${TWO}nd

if [ -d "c:/" ]; then REMOTE_SERVER_TYPE="WINDOWS"; else REMOTE_SERVER_TYPE="UNIX"; fi
if [ $REMOTE_SERVER_TYPE = 'WINDOWS' ]; then echo Windowss; else echo heheh; fi
if [ "WINDOWS" = 'WINDOWS' ]; then echo Windows1; else echo heheh; fi
if [ "WINDOWS" = $REMOTE_SERVER_TYPE ]; then echo Windows2; else echo heheh; fi
if [ WINDOWS = $REMOTE_SERVER_TYPE ]; then echo Windows2; else echo heheh; fi

练习的小内容:

判断当前文件夹下是否存在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                                               分号 ;不能少,否则报错:syntax error: unexpected end of file   ;不要分号则then 换行写;

更复杂的情况,则可以使用这个语法:
if [ 条件判断式一 ]; then
当条件判断式一成立时,可以进行的指令工作内容;
elif [ 条件判断式二 ]; then
当条件判断式二成立时,可以进行的指令工作内容;
else
当条件判断式一与二均不成立时,可以进行的指令工作内容;
fi


文件表达式


条件表达式
if [ -f  file]   如果文件存在
if [ -d...  ]   如果目录存在
if [ -s file ]   如果文件存在且非空
if [ -r file ]   如果文件存在且可读

if [ -w 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 ]           如果string 非空(非0),返回0(true)
if  [ -z $string ]          如果string 为空
if  [ $sting]                 如果string非空,返回0 (和-n类似)
   逻辑非!                                     条件表达式的相反
if [ ! 表达式 ]
if [ ! -d $num]         如果不存在目录$num


   逻辑与–a                                  条件表达式的并列
if [ 表达式1  –a  表达式2 ]


    逻辑或-o                                 条件表达式的或
if [ 表达式1  –o 表达式2 ]

  
   逻辑表达式
   表达式与前面的=  != -d –f –x -ne -eq -lt等合用
   逻辑符号就正常的接其他表达式,没有任何括号( ),就是并列
               if [ -z "$JHHOME" -a -d $HOME/

        注意逻辑与-a与逻辑或-o很容易和其他字符串或文件的运算符号搞混



code:

  #!/bin/bash
  
   filename=/home/yee/if/a.txt
   if [ -f $filename ]; then
           touch a-1.txt
   fi
   if [ ! -e $filename ]; then
           touch t.txt
   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
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     等价于   if [ $num –eq 0]



test 表达式,没有 [  ]


[plain] view plaincopy
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       
[plain] view plaincopy
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 <span style="color:#FF0000;">16:11</span> 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 <span style="color:#FF0000;">16:20 </span>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#  




echo -e参数使输出中的反斜线(\)的说明起作用
echo -n参数使引号后的内容接着输出(不换行)

猜你喜欢

转载自az12xc34.iteye.com/blog/1751556
今日推荐