歡迎您光臨本站 註冊首頁

Linux系統shell腳本判斷變數文件目錄:許可權、是否存在、空值、相等 .

Linux系統shell腳本判斷變數文件目錄:許可權、是否存在、空值、相等 .






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
《解決方案》

謝謝分享
《解決方案》

:victory:

[火星人 ] Linux系統shell腳本判斷變數文件目錄:許可權、是否存在、空值、相等 .已經有4269次圍觀

http://coctec.com/docs/service/show-post-915.html