I wanted to know where it is changing the counter variable, I use it to count mail addresses contained in a file, when I print out the cycle, the value disappears from the terminal, on the other hand if the print within the cycle, the value is correct. So I do not understand where the value is lost, and I must print out the cycle as it is right
cat $FILENAME_2 | while read LINE
do
if [ "$LINE" = "" ]
then
echo "blanks"
exit 1
fi
TEST=${LINE:0:1}
//verify string start with #
if [ "$TEST" != "#" ] ; then
let CONTADOR=CONTADOR+1
cat html_list | mail -a "Content-type:text/html" -s "$ASUNTO" $LINE
echo "Mail enviado a $LINE"
fi
echo "i send $CONTADOR mails" //print mails count
done
echo "i send $CONTADOR mails" // print blank space
The value of the variable is lost because when you use |
you fork a new subshell. The variable CONTADOR
is created in the sub-shell. When you come out of it (ie after done
) that variable holds no value.
The right way to read a file is
while read LINE; do
# do stuff here
done < "$FILENAME_2"
Read more about using a while loop
for reading data at BashFAQ/001 .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.