一些基本的shell脚本

时间:2021-06-29 21:47:29

IF 语句

if  condition

then

        statements

        ...

[ elif condition

    then

    statements

... ]

[ else

 

statements

 

... ]

 

fi

其中“[]”是test命令并不是必须的

if grep foo myfile &> /dev/null;then

for example:

#!/bin/bash

DIR="/tmp"

if  test  $DIR ; then

        echo "This is  a directory"

else

        echo "This isnot a directory"

fi

上面的例子也可以写成如下:

 

#!/bin/bash

DIR="/tmp"

if  [  -d  $DIR  ];then

       

        echo "This is  a directory"

else

        echo "This isnot a directory"

fi

 

扩展 :

#!/bin/bash

DIR="/tmp/"

if test -d $DIR ;then

         echo "this is a dircetory"

elif  test -f $DIR ; then

         echo "this is a file"

else

        echo "this is neither a directory nor a file"

fi

本文出自 “赶不上的脚步” 博客,请务必保留此出处http://xxmspace.blog.51cto.com/1056016/1546636