问题
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
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.
回答1:
You should just initialize file with
FILE=${1:-/var/www/iphorex/live/infi_script.txt}
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.)
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
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.
回答2:
For bash (and ksh and others), you want [[ "$x" == "$y" ]]
with double brackets. That uses the built-in expression handling. A single bracket calls out to the test
executable which is probably barfing on the ==.
Also, you can use [[ -z "$x" ]]
to test for zero-length strings, instead of comparing to the empty string. See "CONDITIONAL EXPRESSIONS" in your bash manual.
来源:https://stackoverflow.com/questions/4921057/shell-script-working-fine-on-one-server-but-not-on-another