Stories
Slash Boxes
Comments

SoylentNews is people

SoylentNews is powered by your submissions, so send in your scoop. Only 16 submissions in the queue.
posted by LaminatorX on Sunday October 12 2014, @09:19AM   Printer-friendly
from the Bad-Ass-Script-Host dept.

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/

 
This discussion has been archived. No new comments can be posted.
Display Options Threshold/Breakthrough Mark All as Read Mark All as Unread
The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
  • (Score: 4, Informative) by maxwell demon on Sunday October 12 2014, @12:15PM

    by maxwell demon (1608) on Sunday October 12 2014, @12:15PM (#105035) Journal

    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.
    Starting Score:    1  point
    Moderation   +2  
       Insightful=1, Informative=1, Total=2
    Extra 'Informative' Modifier   0  
    Karma-Bonus Modifier   +1  

    Total Score:   4  
  • (Score: 3, Informative) by toygeek on Sunday October 12 2014, @08:46PM

    by toygeek (28) on Sunday October 12 2014, @08:46PM (#105256) Homepage

    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