Operating System - HP-UX
1833310 Members
2655 Online
110051 Solutions
New Discussion

ksh while read loop returns nothing

 
SOLVED
Go to solution
Ratzie
Super Advisor

ksh while read loop returns nothing

I am having a heck of at time on a while look in ksh.
I can cat the file in the script one step above and it returns data, on entry per line, but when I try to do a while loop, even the echo $i returns nothing.

...
cat $TICKETS_IN_AC_LOG

cat $TICKETS_IN_AC_LOG|while read i
do
echo $i
var=`grep $i $TICKETS_COMPLETE_IN_WFMS_LOG`
if [[ $var -eq 0 ]]
then
echo $i >>$TICKETS_MISMATCH
fi
done
...



7 REPLIES 7
Steven Schweda
Honored Contributor

Re: ksh while read loop returns nothing

> [...] in ksh.

On what?

uname -a

You might find more people who would look at
a complete (small) test script than will try
to guess exactly what you're doing.
Dennis Handly
Acclaimed Contributor

Re: ksh while read loop returns nothing

This seems ok. You can make some improvements on it:
while read i; do
echo $i
var=$(grep $i $TICKETS_COMPLETE_IN_WFMS_LOG)
if [[ $var -eq 0 ]]; then
echo $i >> $TICKETS_MISMATCH
fi
done < $TICKETS_IN_AC_LOG

That "if" is suspect. You seem to be searching a file $TICKETS_COMPLETE_IN_WFMS_LOG for a record in $TICKETS_IN_AC_LOG and then checking it for the value 0.
Did you want to check the exit status instead?
James R. Ferguson
Acclaimed Contributor
Solution

Re: ksh while read loop returns nothing

Hi:

This works for me:

...
while read i
do
echo ${i}
var=$(grep -c "$i" ${TICKETS_COMPLETE_IN_WFMS_LOG})
if [ ${var} -eq 0 ]; then
echo ${i} >> ${TICKETS_MISMATCH}
fi
done < ${TICKETS_IN_AC_LOG}


...Notice that we changed the archaic backtick syntax to the POSIX $(...) notation to run a command.

Too, the 'grep -c' returns the *count* of matched items which is then tested. You weren't doing that correctly.

Lastly, we eliminated the extraneous process --- the 'cat'. The shell can do this (as shown) much more efficiently as I wrote it.

Regards!

...JRF...
Ratzie
Super Advisor

Re: ksh while read loop returns nothing

yes, basically check to see if the record exists in $TICKETS_COMPLETE_IN_WFMS_LOG

if it does, put that record in $TICKETS_MISMATCH
Ratzie
Super Advisor

Re: ksh while read loop returns nothing

This sure is puzzling...
while read i; do
echo $i
var=$(grep $i $TICKETS_COMPLETE_IN_WFMS_LOG)
if [[ $var -eq 0 ]]; then
echo $i >> $TICKETS_MISMATCH
fi
done < $TICKETS_IN_AC_LOG

Still wont echo line. and it returns nothing.
Bill Hassell
Honored Contributor

Re: ksh while read loop returns nothing

First, I would treat all the lines as text which means $i is not the same as "$i". Start with the simplest loop:

cat $TICKETS_IN_AC_LOG|while read i
do
echo "$i"
done

This should list the file contents (leading spaces truncated). If that works OK, then grep:

cat $TICKETS_IN_AC_LOG|while read i
do
grep "$i" $TICKETS_COMPLETE_IN_WFMS_LOG
done

Now it is important to note that imbedded spaces and tabs (ie, white space) will NOT always compare equal. Are the strings in $TICKETS_IN_AC_LOG full of spaces? It would help by showing sample contents of both logs. I would extract 5-10 sample records from each and run grep manually until you get the desired results.

Once your grep is working, you can simplify the script to just:

cat $TICKETS_IN_AC_LOG|while read TEXT
do
[[ $(grep -c "$TEXT" $TICKETS_COMPLETE_IN_WFMS_LOG) -eq 0 ]] &&
echo "$TEXT" >> $TICKETS_MISMATCH
done


Bill Hassell, sysadmin
Steven Schweda
Honored Contributor

Re: ksh while read loop returns nothing

> This sure is puzzling...

A complete test case might change that.

Just a thought.