i'm getting the following error when i run the shell script. Can you please help me out. :
compareReports.sh[17]: test: argument expected
compareReports.sh[17]: Difference: unknown test operator
compareReports.sh[17]: test: argument expected
compareReports.sh[17]: test: argument expected
compareReports.sh[20]: test: argument expected
compareReports.sh[20]: Difference: unknown test operator
compareReports.sh[20]: test: argument expected
compareReports.sh[20]: test: argument expected
this is the function in the script :
ftpFromProd() {
GET_CMD=""
while [ true ]
do
read line
if [ $? -ne 0 ];then
break
fi
if [ -z "$line" ] || [ `expr "$line" : "#"` -gt 0 ]
then
(this is the 17th line) continue
fi
fileName=`echo $line | sed -e 's/\*/\\\/g' -e "s@\*@\\\@g" -e "s@BATCHDIR@$tar_batchDir@g" -e "s/RUNDATE/$rd
ate/g"`
(this is th 20th line)echo "FileName->$fileName"
r_dir=`dirname $fileName`
echo "Dir->$r_dir"
file=`basename $fileName`
echo "file->$file"
l_dir="$opdir/$r_dir"
if [ ! -d $l_dir ]; then
mkdir -p $l_dir
fi
GET_CMD="$GET_CMD lcd $l_dir\n cd $r_dir\n mget $file\n"
done < $SCRIPTLOC/config_file.txt
Nope, that's not it, the use of || is correct (but the rest of the script hurts to look at, no offense)
I'm guessing the line numbers are off, can you run it with sh -x compareReports.sh to see where the errors are really coming from?
The error message indicates you have an unquoted variable with an unexpected value in it. You should actually probably put double quotes around all your variables regardless.
whenever you see errors like this, i.e. [17]: [20]:
This corresponds to the line number in the script, where the error happened, so your error might not be in that function but elsewhere, you didnt list the entire script so is hard to tell.
you can:
vi +17 scriptname
vi +20 scriptname
# to take you to either of those exact lines.
If it says "function: not found" they it doesn't understand the function keyword. Take it out, it's not needed (and not part of Bourne syntax as per POSIX).
Often, it means you have not quoted a variable properly. The command test is also known as [ (that's right, opening square bracket). Anywhere you see something like
if [ $variable -ne "foo" ]
... you want to add proper quoting, like
if [ "$variable" -ne "foo]
Ditto for where you have a literal "test" command, of course. See also UNIX Shell Quote and the responses people have posted earlier in this thread.
If you can't figure it out yourself, please post the code around lines 17 and 20, as it appears to contain a syntax error. "Difference" is probably the content of the unquoted or otherwise problematic variable, but of course, if you have a literal string "Difference" in your script then that, too, would be interesting to see.