shell脚本一台服务器,但不是在另一个工作正常 [英] shell script working fine on one server but not on another

查看:110
本文介绍了shell脚本一台服务器,但不是在另一个工作正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的脚本工作正常一台服务器上,但在另一方面,它提供了一个错误

the following script is working fine on one server but on the other it gives an error

#!/bin/bash

processLine(){
  line="$@" # 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 绝对是present。我让我的亚马逊服务器上的下面的错误 -

i am just trying to read a file containing path of various scripts and then checking whether those scripts are already running and if not running them. The file /var/www/iphorex/live/infi_script.txt is definitely present. I get the following error on my amazon server-

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

感谢您的帮助提前。

Thanks for your helps in advance.

推荐答案

您应该只初始化文件,


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

然后跳过存在检查。如果文件
不存在或无法读取,则EXEC 0℃;将
失败,一个合理的错误消息,(有没有点
在你试图猜测该错误信息是什么,
只是让壳报告错误。)

and then skip the existence check. If the file does not exist or is not readable, the exec 0< will fail with a reasonable error message (there's no point in you trying to guess what the error message will be, just let the shell report the error.)

我认为问题是,发生故障的服务器上壳
在相等测试不喜欢==。 (许多实现
测试只接受一个'=',但我认为更老的bash
不得不接受了两个==',所以我可能是大错特错一个内置)。
我只想消除FILE你的线条=+下调至
存在的最后检查,并与替换它们
上述转让,让壳的标准默认
为你的工作机制。

I think the problem is that the shell on the failing server does not like "==" in the equality test. (Many implementations of test only accept one '=', but I thought even older bash had a builtin that accepted two '==' so I might be way off base.) I would simply eliminate your lines from FILE="" down to the end of the existence check and replace them with the assignment above, letting the shell's standard default mechanism work for you.

请注意,如果你做消除存在的检查,你会想
以添加

Note that if you do eliminate the existence check, you'll want to either add


set -e

接近脚本的顶部,或在EXEC增加一个检查:

near the top of the script, or add a check on the exec:


exec 0<"$FILE" || exit 1

这样脚本不如果文件无法使用下去。

so that the script does not continue if the file is not usable.

这篇关于shell脚本一台服务器,但不是在另一个工作正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆