When I first learned about Linux in the 90’s, I read that it was possible to even write your own commands to use at the command line. Later I learned about bash scripting, and it wasn’t long before I needed to learn how to loop in bash. Looping in bash is one of the fundamental building blocks of bash programming. It isn’t hard to do at all and is worth learning. The main reason to learn looping in bash is to handle doing the same thing over and over again. They’re easy to do even at the command line. Please follow along as we look a couple of basic examples, and how you can expand on them.
http://www.tidbitsfortechs.com/2014/10/looping-in-bash/
(Score: 4, Informative) by maxwell demon on Sunday October 12 2014, @12:15PM
Indeed, it is not even good as tutorial, as it does several things not advisable.
First, the constructs he uses are just regular Bourne shell constructs, yet he uses #!/bin/bash as shebang line. Doing so unnecessarily restricts portability (and since the Shellshock bug should also be avoided for security reasons). He should use #!/bin/sh for portability.
Next, it uses backticks instead of $() for command substitution. Unless you need to run your code on very ancient shells, don't do that. It's too easy to get wrong, and then get unexpected behaviour (a long time ago, I once even inadvertently created a fork bomb this way — I just wanted to output a lot of information from the script, including the script's own name, and an error in the backticks actually caused one of the $0 directly following an opening backtick (which was intended to be a closing one) ...
Next, he is using variables completely without quotes. That's just asking for disaster when any name has a space inside. Now you may argue that the for construct he uses won't work of he puts $names in quotes. But then, the whole idea of storing the output of cat in a variable via command substitution. The right loop to use here would be a while loop using read to get the names from the file.
Also, why does he use /bin/true instead of simply true, although he uses cat and not /bin/cat? Obviously /bin is in his path, or cat would not be found. Not to mention that bash provides a built-in true which almost certainly is more efficient to use that /bin/true.
The Tao of math: The numbers you can count are not the real numbers.
(Score: 3, Informative) by toygeek on Sunday October 12 2014, @08:46PM
OP here. I can address all of your concerns with two words: Simplicity, Habit. Thanks for reading :)
There is no Sig. Okay, maybe a short one. http://miscdotgeek.com