This is a really useful bit of code which I use almost daily to speed up routine tasks. You can pipe text, split into multiple lines, into a while loop and read through them line by line. This is how you do it:
Read a file line by line and echo (reversed) the line:
cat file.txt | while read line
do
echo $line | rev
done
do
echo $line | rev
done
Read the output of an ls and cat each file through sed:
ls -1 | while read line
do
cat $line | sed 's/foo/bar/g'
done
do
cat $line | sed 's/foo/bar/g'
done
You’ll see that you ‘while read XXXX’. $XXXX then becomes the variable that references that line. So in the above example ‘while read line’ produces $line. Naturally, there are better ways to do the above tasks but it hopefully gives you an idea of the power of a while loop in bash.
Just to let you know, both of those examples contain what is known as “useless uses of cat”. It shows bad coding practice for bash scripts, opening up more processes than are necessary. Google ‘useless uses of cat’ for more information on this common faux-pas.
The following does the same, without the waste of a cat:
while read line; do
echo $line | rev
done < file.txt
#==========
ls -1 | while read line
do
sed 's/foo/bar/g' < $line
done