Check condition inside the loop

Hi,

I am in trouble. I can get inside my condition test inside a loop :

I am in ksh (solaris)

while read file
do
<commande to retrieve file> >> ${LOG_RETRIEVE_FILE.log}
msg_err=$(cat ${LOG_RETRIEVE_FILE.log} | grep "error retrieve")
if [ ${msg_err} -gt 0 ] ; then
<sendmail>
exit 1
fi
done

I tried different options, but none were working.

thanks for your help

msg_err=$(cat ${LOG_RETRIEVE_FILE.log} | grep "error retrieve")

replace it to 

msg_err=$(grep -c "error retrieve" ${LOG_RETRIEVE_FILE.log})  

Thnaks for your help, but this is not working ....

It does return value if i ma using it manually but not in the shell script ... :wall:

while read file
do
<commande to retrieve file> >> ${LOG_RETRIEVE_FILE.log}  #what exactly you doing here? 
msg_err=`grep -c "error retrieve" $LOG_RETRIEVE_FILE.log`
echo $msg_err
if [ "$msg_err" -gt "0" ] ; then
echo "send email"
exit 1
fi
done < inputFile  #where is your input for the while loop ?
1 Like

Maybe he's reading file from keyboard input?

msg_err=$(cat ${LOG_RETRIEVE_FILE.log} | grep "error retrieve")
if [ ${msg_err} -gt 0 ] ; then

msg_err will most likely be a string, not a number, something along error retrieve [a number maybe...?]
So you can't use -gt to test if error retrieve [something] is greater than 0
Can you post some lines of your log file? (namely those with 'error retrieve' )

1 Like

If you are only interested about grep true/false, not count then

if grep "error retrieve" $LOG_RETRIEVE_FILE.log >/dev/null 2>&1
then
     <sendmail>
     exit 1
fi

Because grep exit status is 0 if find something.

Variable name LOG_RETRIEVE_FILE or LOG_RETRIEVE_FILE.log ?
${LOG_RETRIEVE_FILE}.log or ${LOG_RETRIEVE_FILE.log}

1 Like

Thanks all for your help.

I found the error on my script. As I check the return code while connecting to the archive system to retrieve files on the top of the code, I was unable to get inside of the condition in my loop.

I am trying to find a way to solve this on my own, but thanks again for your help.