I am encountering a problem when I try to run a script in RedHat OS. I get an arithmetic syntax error.

enter image description here

Now here's the script that I am trying to run:

#!/bin/ksh

echo ""
echo " = K+WA =";
WA_PORT="`cat /usr/kplushome/entities/Standalone/config/kondor.active |grep PORTAL_PORT|cut -d '&' -f 2`"

#== Checking Tomcat of WebAccess
printf "%25s" Tomcat
touch /usr/kplushome/entities/Standalone/config/webaccess/WebaccessDomainTomcat/bin/tomcat-*.pid
PID=`cat /usr/kplushome/entities/Standalone/config/webaccess/WebaccessDomainTomcat/bin/tomcat-*.pid`
if [[ $PID = "" ]];then
echo " x 0"
else
PSPID=`/bin/ps -eo pid | grep -w $PID`
if [[ $PSPID -eq $PID ]];then
/bin/ps -ef | grep $PID | grep -v grep | awk 'NR==1 {print " = "$8 $9 $10 $11 $12 $13 $14 $15}'
else
echo " x 0"
fi
fi

PORT_STATUS=`netstat -an | grep ${WA_PORT} | grep LISTEN | perl -pe "s/^.+\n/LISTENING/g;"`
PORT_STATUS=${PORT_STATUS:="NOT LISTENING!!!"}
echo " Port ${WA_PORT}/TCP = ${PORT_STATUS}"

cd /usr/kplushome/entities/Standalone/config/webaccess/WebaccessServer/etc
(PATH=/usr/ucb:$PATH; ./checkall.sh;) | perl -pe "s,^, ,g;"
echo ""

Problem is I don't see anything wrong with my line 15 as I don't see any rule from unix that was violated for it to have an arithmetic syntax error, or am I missing something? May I ask for any advice from you experts? Any help would be much appreciated. Thank you.

  • 1
    Could be that any of $PSPID or $PID is empty at line 15 at script runtime? – coffeMug Feb 15 '16 at 8:28
  • that I am not sure, is there a way for me to check whether line 15 is empty at script runtime or not? please help, thank you! – Francis Feb 16 '16 at 3:27
  • Put an echo at line 14: echo "PSPID is:$PSPID and PID is:$PID". Something like that. – coffeMug Feb 16 '16 at 9:40
up vote 0 down vote accepted

The error message shows two numbers. These seem to be process ids. A likely answer is that your line

PID=`cat /usr/kplushome/entities/Standalone/config/webaccess/WebaccessDomainTomcat/bin/tomcat-*.pid`

is finding 2 files and you are getting these 2 pids set in the variable, separated by a newline. When used in if [[ $PSPID -eq $PID ]] you will have 2 numbers after the -eq, which is the syntax error.

You should doublequote your variables (eg "$PID") to avoid this type of syntax error, though clearly the real problem is that you had 2 numbers instead of 1. Depending on what is legal for your situation, you could simply add a loop handling each pid separately (for pid in $PID...).

  • Hi meuh, should I double quote all the $PID in my script or just the $PID from this line: if [[ $PSPID -eq $PID ]] ?? thank you – Francis Feb 15 '16 at 23:17
  • am I supposed to double quote only the $PID in the if [[ $PSPID -eq $PID ]] ? Or should I do it with all the $PID in the script. thanks – Francis Feb 16 '16 at 3:15
  • Good practice is to always double quote all variables when they are used, to avoid whitespace in the value causing it to be split into separate words, so quote PSPID and PID to avoid syntax errors. However, in some cases you know the value consists of many words and you want to split them, as in this example where you wouldn't quote for pid in $PID if you use it. – meuh Feb 16 '16 at 6:56
  • I think you're first assessment is correct, there were two files with tomcat- at the beginning of their filenames that were saved in the system namely tomcat-.pid and tomcat-KOPL58IRA02.pid. I plan to remove the wildcard * in line 10 so instead of tomcat-*.pid I will change it to tomcat-.pid so that it will only access one file. Do you think my solution will solve the problem? I'm asking right now since I do not have the machine at the moment so I cannot test it currently – Francis Feb 17 '16 at 4:17
  • I forgot to mention, those two files contain the same process id: 1743 so it's a bit clever why they are separate files when they just contain the same thing :) – Francis Feb 17 '16 at 4:27

Lines 14 and 15 of the script are:

PSPID=`/bin/ps -eo pid | grep -w $PID`
if [[ $PSPID -eq $PID ]];then

If you're getting an error on line 15, the most likely cause is that either PSPID or PID is undefined, likely resulting in an effective line 15 of something like:

if [[ -eq 12345 ]];then

which would be a syntactical error. You can mitigate this by quoting your variables (e. g. "$PID" rather than $PID).

As an aside, it's helpful to see what's going on as your script runs. It does munge the output a bit, but for problematic sections of your script, prefix what's misbehaving with set -x and your standard output will now include command-by-command what is being sent to the shell that's running the script.

  • hi expert, how should I do that prefix? is it supposed to be like this: set -x if [[ $PSPID -eq $PID ]];then ?? thanks – Francis Feb 16 '16 at 3:14
  • set -x would be on its own line before the if block, and then set +x later to turn it back off. – DopeGhoti Feb 16 '16 at 6:03

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.