I guess this may be a naive question but I can't get my head around so I felt like asking...
I was searching for some solution to a problem, when I found this very interesting post about why is using [while|for]
loops in bash considered bad practice. There is a very good explanation in the post (see the chosen answer) but I can't find anything that solves the issues that are discussed.
I searched extensively: I googled (or duckduckgo-ed) how to read a file in bash
and all the results I am getting point towards a solution that, according to the above-mentioned post, is absolutely non-bash style and something that should be avoided. In particular, we have this:
while read line; do
echo $line | cut -c3
done
and this:
for line in `cat file`; do
foo=`echo $line | awk '{print $2}'`
echo whatever $foo
done
that are indicated as very bad examples of shell scripting. At this point I am wondering, and this is the actual question: if the posted while loops should be avoided because they are bad practice and whatever...what am I supposed to do, instead?
EDIT: I see that I am already having comments/questions addressing the exact issue with the while
loop, so I feel like to widen the question a bit. Basically, what I am understanding is that I need to dig deeper into bash commands, and that is the real thing that I should do. But, when one searches around, it looks like people are, in the general case, using and teaching bash in an improper way (as per my google-ing).
cut
orawk
on the file directly. In general: There is no general answer.