Things you learn the Hard Way

Miscellaneous

ad free blog

2007-08-13 by Thomas Fischer [en]

Thomas Fischer There are things in life that you learn the hard way. The same holds true for Linux and, in the following example, for bash scripts.

Lets have a look on the following two code examples:

counter=0
while read line ; do
    echo $line | grep a >/dev/null || continue
    counter=$(( $counter + 1 ))
done < test.txt
echo $counter
and
counter=0
grep a < test.txt | while read line ; do
    counter=$(( $counter + 1 ))
done
echo $counter
given a text file test.txt containing the lines
Apple
Banana
Orange
Cheese

As it turns out, in the second case the variable counter is always reset to 0, regardless of the hits in test.txt, whereas the first example has a final result of 2 as expected. It seems, that changes to counter do not propagate to the world outside the while-do-loop, if the output of grep is piped into the loop.
I'm not sure why this happens, but it can be reproduced both on a Gentoo and a Fedora box.

Although the examples above are artificial, the problem occurred within a larger script and it took me three days to both notice that the problem exists and to find the reason... :-P

Keywords: Linux, University
Trackback-URL: http://www.t-fischer.net/blog/20070813_Things_you_learn_the_hard_way

Writing comments is currently not possible. Feel free to write me an email to the address shown on my homepage.

More social bookmarking links than anybody else ;-)
LinkARENAalltagzTechnoratiMister Wongdel.icio.usGoogle BookmarksYiGGsimpyfurlblogmarkstailranktektagoneviewdiggslashdotfolkdyahoo my web
Valid XHTML 1.0!Valid CSS!UTF-8 EncodedUse Metric & SIGentoo LinuxSession Initiation ProtocolAny BrowserWir wollen keine Softwarepatente!Coffee PoweredUnix-AGKDE Developer (KBibTeX)Hacker EmblemPlay OGGXING profileDANTE MemberThe content of this page is licensed under a Creative Commons Attribution-Share Alike 3.0 License, unless noted otherwise.Blog
Date of Modification: 2009-Feb-21 18:08Chuck Norris