I wrote a script, including this loop:
#!/bin/bash
cat "$1" | while read -r line; do
echo "$line"; sleep 2;
done
A shellcheck run put out the following message:
SC2002: Useless cat. Consider 'cmd < file | ..' or 'cmd file | ..' instead.
I changed the script to:
#!/bin/bash
cmd < "$1" | while read -r line; do
echo "$line"; sleep 2;
done
but now bash exits with:
cmd: command not found
what have I done wrong?
cmdis a placeholder for the thing you're really running. In this case, that's thewhileloop.cat foo | awk, it would be telling you to runawk <fooorawk foo-- thecmdwould beawk.<filename while ...; I'm not sure ifwhile ... done <filenameworks, but for a long command it's not that readable to push the input filename all the way to the end. It's a matter of opinion, I guess, but ifcatkeeps the pipeline in order, that's a big plus point in my book.cmd < file |makes it look likecmdshould just replacecat. But that means that my first example is not bad programming practise from your point of view.while read, but it's a pretty convenient idiom.