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