Categories
Linux Nix Shell Scripting Unix

Loopy scripts!

Learning how to use a loops in any language is the WOW point for most people. That moment when they go from “I don’t see the point in this” to “I can use this to rule the world.” Most programing languages have three loops and they are similar or identical in what they do and push programmers to lean towards using for. They are for while and until. When programing in C they are almost identical. GoLang decided it was silly to have three commands doing the same thing and threw out while and until to just use for.

That brings us to bash which of course, can’t do things like anyone else and just has to be different. This is why there will be so much confusion when I make this statement: “for is not a loop in bash.” By understanding why you can improve the effectiveness of your scripts.

Ok, let’s back up and look at the actual looping statements in bash: while and until. I’m going to talk about while as the two commands do exactly the same thing from the computers perspective. When you build a while statement, you test your environment, if it’s true you complete the tasks within the statement, if it’s false you stop. Consider each time you test your environment, the script has no idea how many times the loop will run and has no real destination. It pulls in one idea at a time, tests it and continues.

Now, compare this to a for command. A for command uses a list and goes through it one item at a time running a series of commands for each item. When you build the statement, there is a beginning and an end. It runs a series of commands for that list. Rather than a loop, think of a for command as a list parser.

Consider now what that means for the computer. When your for command runs, memory is allocated for your entire input. The whole list, no matter what is in it goes into memory then each item is evaluated. The while command has no idea where it’s beginning or end is. As a result, it can only possibly hold what it’s currently doing.

So which should you use? Both! Now that you understand how each command works you can use them more effectively. Most of the time, if you have a list of small items with a defined beginning and end, it will be best to use a for statement. If you have very large items to examine or you cannot determine the beginning and end you will want to use while or until. Like I said, this isn’t always the case but it’s a good rule of thumb to go by.

Afterwards:

  • This is only the case for shell as far as I know. In every other language I’ve used for is a loop.
  • Sometimes the only way to see what is best for your environment and script is to test it each way especially if its a script you will be running often (such as a daily cron)
  • Stay away from the break command within for and while if possible; it doesn’t always tear down your commands (and subshells) cleanly. You should be able to write your test well enough that it’s not needed. Be nice to your shell!

Leave a Reply

Your email address will not be published.