The next script campares two files File1-Line1 vs File2-Line1, File1-Line1 vs File2-Line2... only if line contains "AS-", if LineX is not in File2 writes in aux, but "valor" is allways=1 never changes! What is wrong?
valor changes to 0 before break, after brake is again 1
#!/bin/ksh
echo > aux
while read label
do
valor=1
echo $label | grep "AS-"
if [ $? -eq 0 ]
then
while read labelold
do
if [ "${label}" = "${labelold}" ]
then #echo "encon" >aux2
valor=0
echo $valor
break
fi
echo $valor #<--- Allways is 1
done < hosts.old
if [$valor -eq 1]
then
echo "$label" >> aux
fi
fi
done < hosts
Whenever you pipe things into a loop, that loop becomes its own process, with it's own seperate process space -- which inherits variables you already have, but things it sets don't propogate back.
#!/bin/ksh
echo > aux
while read label
do
valor=1
echo $label | grep "AS-"
if [ $? -eq 0 ]
then
while read labelold
do
if [ "${label}" = "${labelold}" ]
then
#echo "encon" >aux2
valor=0
echo $valor
break
fi
echo $valor #<--- Allways is 1
done < hosts.old
if [$valor -eq 1]
then
echo "$label" >> aux
fi
fi
done < hosts
After assigning the value 0 to your variable, you break the while loop so you never display the variable with the new value 0.
Whith KSH the modification of variable inside loops aren't lost (that is not the case with bourne shell).
Try the following script:
[CODE]var=init
ls | while read file
do
var="Last file is $file"
done