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: 2, Insightful) by Rosco P. Coltrane on Sunday October 12 2014, @09:53AM

    by Rosco P. Coltrane (4757) on Sunday October 12 2014, @09:53AM (#105008)

    Not to nitpick or anything - it's great that people discover new and exciting things - but how is this news, let alone interesting news? This is shell scripting 101...

    I was expecting an article on loopHOLES in Bash, not an introductory course on basic algorithmics.

    Starting Score:    1  point
    Moderation   +1  
       Insightful=1, Total=1
    Extra 'Insightful' Modifier   0  

    Total Score:   2  
  • (Score: 0) by Anonymous Coward on Sunday October 12 2014, @10:34AM

    by Anonymous Coward on Sunday October 12 2014, @10:34AM (#105016)

    its interesting and nerdy, and news shouldn't just be about sensationalized bs... there's plenty of other sites for that

    SN is about community as well as news, and sharing tips and tricks, reviews, asking questions etc are all perfect for engaging discussion

    everyone is welcome to also share their opinion of course, which is also great

  • (Score: 0) by Anonymous Coward on Sunday October 12 2014, @10:41AM

    by Anonymous Coward on Sunday October 12 2014, @10:41AM (#105017)

    Not to nitpick or anything - it's great that people discover new and exciting things - but how is this news, let alone interesting news? This is shell scripting 101...

    Indeed. LaminatorX should have done a little bit of research before posting this "news story". A simple google search for "bash script" would have turned up millions of hits. This is not news. Not even close.

    • (Score: 2) by theluggage on Sunday October 12 2014, @11:07AM

      by theluggage (1797) on Sunday October 12 2014, @11:07AM (#105023)

      This is not news. Not even close

      But... "bash" is a trending keyword at the moment, and surely the language every l33t haX0r wants to learn right now. I hope part 2 of this article covers how to embed loops in environment variable declarations.

      P.S. Heartbleed had a logo. Where's the shellshock logo? I thought every security vulnerability got a snappy name and a logo now?

      • (Score: 0) by Anonymous Coward on Sunday October 12 2014, @02:38PM

        by Anonymous Coward on Sunday October 12 2014, @02:38PM (#105057)

        Here you go... [twimg.com]

      • (Score: 0) by Anonymous Coward on Monday October 13 2014, @02:08PM

        by Anonymous Coward on Monday October 13 2014, @02:08PM (#105557)
        Given the security and code quality problems with bash perhaps we should start learning to write stuff so that it's not bash specific.
    • (Score: 2, Insightful) by Anonoob on Sunday October 12 2014, @12:11PM

      by Anonoob (335) on Sunday October 12 2014, @12:11PM (#105034)

      The 'millions of hits' itself is important point here - even the few comments so far have given those of us in the 101 camp some insights, that may not be immediately resolved fishing through those hits. Can readily agree it isn't news, but who really comes to SN for the articles. Sure I can go learn this on other sites, but the opinions it was basic non-news actually peaked my interest, while later comments on different techinques were something the article itself did not go into and shows the true value of this site.
      So I for one, would appreciate this kind of article being posted now and then.

      • (Score: 2) by Phoenix666 on Monday October 13 2014, @01:38PM

        by Phoenix666 (552) on Monday October 13 2014, @01:38PM (#105539) Journal

        while later comments on different techinques were something the article itself did not go into and shows the true value of this site.

        I second this, too. Most of the time when you don't fish in certain waters all the time you chug through a tutorial to get what you need, and move on. This kind of focused feedback gives very valuable context to scripting in general that you would be hard pressed to get any way other than by sitting among the greybeards in the dark rooms for several years.

        --
        Washington DC delenda est.
  • (Score: 5, Insightful) by BsAtHome on Sunday October 12 2014, @11:22AM

    by BsAtHome (889) on Sunday October 12 2014, @11:22AM (#105026)

    While I can relate to the sentiment of this not being news, this site should welcome diversity. Please remember that you also needed to learn the basics at one point...

    The article surely fits into the category "Stuff that matters". New users want to be appreciated too and certainly not mocked for not knowing. They want to learn, you have also learned it and others need to be brought into the breach. We all should politely help each other at the level we can understand and operate. There is no point at making this an exclusive site if you want to reach a broad target audience.

    Just my 2 (euro) cents.

  • (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.
    • (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
  • (Score: 2) by Phoenix666 on Sunday October 12 2014, @12:51PM

    by Phoenix666 (552) on Sunday October 12 2014, @12:51PM (#105040) Journal

    I welcome this kind of thing because bringing back an element of the old LUGs is a good thing. It sets the site apart from other mindless aggregators. Also, when it comes to technology it's always good to get refreshers, even on basic things. Let's say you haven't done any bash scripting in, oh, ten years. It's good to see it again, and it happens also not infrequently that you can learn something new, some trick, that you never knew but wish you had all this time.

    A couple of years ago I had some down time and read a tutorial somewhere about vim scripting. I had once dipped my toe in but since I didn't have a particular use for it at the time never went any further and didn't retain anything. But now I find its use makes me three times more productive and that's awesome.

    --
    Washington DC delenda est.