I have trouble passing a variable from inside a loop.
Code:
# find all output.txt that has been modified last 24h ...
PROCESSED=1
find ${PROCESSED_DIR} -mtime -1 -name "output.txt" | while read i
do
# .. and compare those with TMP_TXT
if diff $i ${TMP_TXT} > /dev/null
then
# If both are same EXIT search loop
PROCESSED=0
exit
fi
done
echo "PROCESSED=${PROCESSED}"
This will always output 1. Any idea how to make PROCESSED=0 ?
This is done on a Solaris 9 machine.
The reason is that the
while-command is run inside a pipe which means that it is running inside a sub-shell, so variable assignments are not propagated to the calling shell.One solution is to redirect the input (you may need to write it to a temporary file first):
Another solution is to use the stdout from the while-command to propagate the value of
PROCESSED: