一些基本的shell脚本

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

你可能感兴趣的:(shell,脚本,if,语句)