一些基本的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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章