原文地址:http://blog.csdn.net/w13770269691/article/details/17356549
1、字符串判斷
str1 = str2 當兩個串有相同內容、長度時爲真
str1 != str2 當串str1和str2不等時爲真
-n str1 當串的長度大於0時爲真(串非空)
-z str1 當串的長度爲0時爲真(空串)
str1 當串str1爲非空時爲真
2、數字的判斷
int1 -eq int2 兩數相等爲真
int1 -ne int2 兩數不等爲真
int1 -gt int2 int1大於int2爲真
int1 -ge int2 int1大於等於int2爲真
int1 -lt int2 int1小於int2爲真
int1 -le int2 int1小於等於int2爲真
3 文件的判斷
-r file 用戶可讀爲真
-w file 用戶可寫爲真
-x file 用戶可執行爲真
-f file 文件爲正規文件爲真
-d file 文件爲目錄爲真
-c file 文件爲字符特殊文件爲真
-b file 文件爲塊特殊文件爲真
-s file 文件大小非0時爲真
-t file 當文件描述符(默認爲1)指定的設備爲終端時爲真
3、複雜邏輯判斷
-a 與
-o 或
! 非
下面是一些使用實例:
#!/bin/sh
myPath="/var/log/httpd/"
myFile="/var /log/httpd/access.log"
#這裏的-x 參數判斷$myPath是否存在並且是否具有可執行權限
if [ ! -x "$myPath"]; then
mkdir "$myPath"
fi
#這裏的-d 參數判斷$myPath是否存在
if [ ! -d "$myPath"]; then
mkdir "$myPath"
fi
#這裏的-f參數判斷$myFile是否存在
if [ ! -f "$myFile" ]; then
touch "$myFile"
fi
#其他參數還有-n,-n是判斷一個變量是否是否有值
if [ ! -n "$myVar" ]; then
echo "$myVar is empty"
exit 0
fi
#兩個變量判斷是否相等
if [ "$var1" == "$var2" ]; then
echo '$var1 eq $var2'
else
echo '$var1 not eq $var2'
fi
if list then
do something here
elif list then
do another thing here
else
do something else here
fi
EX1:
#!/bin/sh
SYSTEM=`uname -s` #獲取操作系統類型,我本地是linux
if [ $SYSTEM = "Linux" ] ; then #如果是linux的話打印linux字符串
echo "Linux"
elif [ $SYSTEM = "FreeBSD" ] ; then
echo "FreeBSD"
elif [ $SYSTEM = "Solaris" ] ; then
echo "Solaris"
else
echo "What?"
fi #ifend
基本上和其他腳本語言一樣。沒有太大區別。不過值得注意的是。[]裏面的條件判斷。
=========================================================================
#. $HOME/.bash_profile
cd $HOME
sh .bash_profile
cd $HOME/jnjzapp/sos_dx/
echo -e '開始'
./jn_card_dx.sh
wait
#sqlplus $DBSTR <<START_A
#@jn_card_dx.sql
#/
#commit;
#exit;
#START_A
cd $HOME/jnjzapp/sos_dx/data/
if [ ! -d BQMSG ]; then
mkdir BQMSG
fi
#mkdir BQMSG
cd $HOME/jnjzapp/sos_dx/
sqlplus $DBSTR <<START
@sos.sql;
commit;
exit;
START
objdir=`date -d ' days' +%Y%m%d`
echo $objdir
#cp jn_card_sos.txt $objdir.txt
1、if 與[ 之間必須有空格
2、[ ]與判斷條件之間也必須有空格
3、]與; 之間不能有空格
原文:http://cloudmail.iteye.com/blog/1441015