for loop non-ending, loop never procedes to second argument

0

I am trying to use the "for name in" to loop through a list of values. It uses the first value in the line but sticks in a never-ending loop.

#!/bin/ksh
set -x
# created 7 JAn 2013 by L Cooper
# purpose is to verify completion of QAD online backup. If incomplete then email personnel
#EMAILTO=mfc_support@manitowoc.com
EMAILTO=lee.cooper@manitowoc.com
TEST=1740               # Success code
SVR=IBM2
LOGDIR=/tmp             #Log location
LOGS="onlineeuro onlinedata online"             # logs to check
# Check QAD online backup log for code 3740, which indicates successful backup

#for logchk in onlineeuro onlinedata onlinena
for logchk in $LOGS
do
        while [ "$(/usr/bin/tail -n -2 $LOGDIR/$logchk.log | head -n +1 | tail -c 6 | head -c 4)" != "$TEST" ]
        do
           echo $LOGDIR/$logchk
           echo "The QAD online backup $logchk on $SVR may have errors...please check" | mailx -s "***TEST*** There mat be QAD onl
ine backup errors!!" $EMAILTO

        done
done

Output of the execution:

+ EMAILTO=lee.cooper@manitowoc.com
+ TEST=1740
+ SVR=IBM2
+ LOGDIR=/tmp
+ LOGS=onlineeuro onlinedata online
+ /usr/bin/tail -n -2 /tmp/onlineeuro.log
+ head -n +1
+ head -c 4
+ tail -c 6
+ [ 3740 != 1740 ]
+ echo /tmp/onlineeuro
/tmp/onlineeuro
+ mailx -s ***TEST*** There mat be QAD online backup errors!! lee.cooper@manitowoc.com
+ echo The QAD online backup onlineeuro on IBM2 may have errors...please check
+ /usr/bin/tail -n -2 /tmp/onlineeuro.log
+ head -n +1
+ head -c 4
+ tail -c 6
+ [ 3740 != 1740 ]
+ echo /tmp/onlineeuro
/tmp/onlineeuro
+ mailx -s ***TEST*** There mat be QAD online backup errors!! lee.cooper@manitowoc.com
+ echo The QAD online backup onlineeuro on IBM2 may have errors...please check
+ /usr/bin/tail -n -2 /tmp/onlineeuro.log
+ head -n +1
+ head -c 4
+ tail -c 6
+ [ 3740 != 1740 ]
+ echo /tmp/onlineeuro
/tmp/onlineeuro
+ mailx -s ***TEST*** There mat be QAD online backup errors!! lee.cooper@manitowoc.com
+ echo The QAD online backup onlineeuro on IBM2 may have errors...please check
+ /usr/bin/tail -n -2 /tmp/onlineeuro.log
+ head -c 4
+ head -n +1
+ tail -c 6
+ [ 3740 != 1740 ]
+ echo /tmp/onlineeuro
/tmp/onlineeuro
+ mailx -s ***TEST*** There mat be QAD online backup errors!! lee.cooper@manitowoc.com
+ echo The QAD online backup onlineeuro on IBM2 may have errors...please check
+ /usr/bin/tail -n -2 /tmp/onlineeuro.log

****** keeps repeating indefinitely ******

Lee Cooper

Posted 2013-01-07T20:03:02.173

Reputation: 1

I just put your code in codeblocks so the formatting doesn't get ruined, please be sure to verify it still looks correct to you! Thanks, and welcome to Super User! – nerdwaller – 2013-01-07T20:07:33.257

1seems the problem is that you loop in the while (ie, it is always true) ( 3740 != 1740 ) – Olivier Dulac – 2013-01-08T05:05:18.403

Answers

-1

Seem that by the while ... do ... done loop your intent was rather to write an "if" block, like this:

if  [ "$(/usr/bin/tail -n -2 $LOGDIR/$logchk.log | head -n +1 | tail -c 6 | head -c 4)" != "$TEST" ]
then
    #stuff
done

BTW (but it's off topic) the "tail, head, tail, head" pipeline seem rather obfuscated to me, which line of the logfile are you interested specifically ?

Gilles Pion

Posted 2013-01-07T20:03:02.173

Reputation: 21