1

This code gets into an infinite loop. How can in troubleshoot it?

PGM=ot_run_hourly
PID=$$
TIMESTAMP=`date +%Y%m%d%H%M%S`
MASTER_DATE_STAMP_BEGIN=`date +%D`
MASTER_TIME_STAMP_BEGIN=`date +%T`
DIR=/var/opt/gogd/ship/na/log

#logfiles /var/opt/gogd/ship/na/log/ot_run_hourly*.log

LOG_FILE=${DIR}/${PGM}_${TIMESTAMP}_${PID}.log
COUNTER=1

date +"Info: %m/%d/%Y %T : Step 1 : Start" | tee -a ${LOG_FILE}

while [ $COUNTER -le 5 ]
do

        date +"Info: %m/%d/%Y %T : Step 2 : Counter $COUNTER" | tee -a ${LOG_FILE}



        COUNTER =`expr $COUNTER + 1`

        sleep 60

done

date +"Info: %m/%d/%Y %T : Step 3 : Complete" | tee -a ${LOG_FILE}/var/opt/gogd/ship/scripts
4

The problem is in the following line:

COUNTER =`expr $COUNTER + 1`

You have a space after COUNTER. You should have seen an error saying:

bash: COUNTER: command not found

Say:

COUNTER=`expr $COUNTER + 1`

instead, or better still

COUNTER=$((COUNTER + 1))
| improve this answer | |
  • I prefer it even shorter: ((COUNTER++)). – manatwork Feb 10 '14 at 11:09
  • Fair enough, I seem to have missed part of the conversation here. Never mind, lets delete this conversation (which I started), we're not adding anything useful to the post. – terdon Feb 10 '14 at 16:15
2

The way to troubleshoot this is by executing the commands before the while loop in a interactive shell, the same that you use to execute this command.

Skip the while statement and then copy and paste

date +"Info: %m/%d/%Y %T : Step 2 : Counter $COUNTER" | tee -a ${LOG_FILE}

and then

COUNTER =`expr $COUNTER + 1`

while checking what the shell tells you COUNTER: command not found and you will notice that echo $COUNTER still gives 1 and never increases. That should give you an idea on where to start looking what to fix.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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