2011-02-07 96 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手册中的“条件表达式”。

相关问题