2011-02-07 97 views
1

以下腳本一臺服務器上工作正常,工作正常,在一臺服務器上,但另一方面,它提供了一個錯誤shell腳本,但沒有其他

#!/bin/bash 

processLine(){ 
    line="[email protected]" # get the complete first line which is the complete script path 
name_of_file=$(basename "$line" ".php") # seperate from the path the name of file excluding extension 
ps aux | grep -v grep | grep -q "$line" || (nohup php -f "$line" > /var/log/iphorex/$name_of_file.log &) 
} 

FILE="" 

if [ "$1" == "" ]; then 
    FILE="/var/www/iphorex/live/infi_script.txt" 
else 
    FILE="$1" 

    # make sure file exist and readable 
    if [ ! -f $FILE ]; then 
    echo "$FILE : does not exists. Script will terminate now." 
    exit 1 
    elif [ ! -r $FILE ]; then 
    echo "$FILE: can not be read. Script will terminate now." 
    exit 2 
    fi 
fi 
# read $FILE using the file descriptors 
# $ifs is a shell variable. Varies from version to version. known as internal file seperator. 
# Set loop separator to end of line 
BACKUPIFS=$IFS 
#use a temp. variable such that $ifs can be restored later. 
IFS=$(echo -en "\n") 
exec 3<&0 
exec 0<"$FILE" 
while read -r line 
do 
    # use $line variable to process line in processLine() function 
    processLine $line 
done 
exec 0<&3 

# restore $IFS which was used to determine what the field separators are 
IFS=$BAKCUPIFS 
exit 0 

我只是想讀取包含的各種路徑的文件腳本,然後檢查這些腳本是否已經運行,如果沒有運行它們。文件/var/www/iphorex/live/infi_script.txt肯定存在。我的亞馬遜服務器上出現以下錯誤 -

[: 24: unexpected operator 
infinity.sh: 32: cannot open : No such file 

感謝您的幫助提前。

回答

3

你應該只初始化文件,

 
FILE=${1:-/var/www/iphorex/live/infi_script.txt} 

然後跳過存在檢查。如果文件 不存在或不可讀,那麼exec 0 <將 將失敗並顯示合理的錯誤消息(您嘗試猜測錯誤消息將會是什麼, 沒有意義, 只是讓shell報告錯誤。 )

我認爲問題是,在故障服務器 上的外殼在等同性測試中不喜歡「==」。 (許多實現測試的 只接受一個「=」,但我認爲更老的bash 不得不接受了兩個「==」,所以我可能是大錯特錯一個內置) 我只想消除FILE你的線條=」 「最後到 存在檢查結束,並用上面的 分配替換它們,讓shell的標準默認 機制爲您工作。

請注意,如果你這樣做消除存在的檢查,你會想 以添加

 
set -e 

附近的腳本的頂部,或在EXEC添加一個檢查:

 
exec 0<"$FILE" || exit 1 

,這樣如果文件不可用,腳本不會繼續。

+0

使用bash命令運行它的工作。你是對的我的其他shell不喜歡==測試。 – ayush 2011-02-07 14:07:23

1

對於bash(以及ksh和其他),您需要帶有雙括號的[[ "$x" == "$y" ]]。這使用內置的表達式處理。一個括號呼叫到test可執行文件,這可能會影響==。

此外,您還可以使用[[ -z "$x" ]]測試,而不是比較空字符串零長度字符串。請參閱bash手冊中的「條件表達式」。

相關問題