Stories
Slash Boxes
Comments

SoylentNews is people

SoylentNews is powered by your submissions, so send in your scoop. Only 19 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) by hemocyanin on Monday October 13 2014, @03:37PM

    by hemocyanin (186) on Monday October 13 2014, @03:37PM (#105586) Journal

    I consider myself beyond introductory scripting, maybe a weak intermediate. I was sort of confused by the example in the original article because there is a lot of similarity between the ` character and the ' character, especially considering how popular it is use funny single and double quotes in websites these days. At first I just thought this was dealing with a file titled: cat names.txt (with the space) so I gave him kudos for dealing with the space in the filename. Anyway, as others have mentioned, I'm much more used to using "while" for this sort of thing.

    names.txt contains
    Joe
    Jack
    John Doe
    Jill Hart
     
    From the TFA (note that even in the article, the letter count is +1):
    $ names=`cat names.txt`; for name in $names; do echo `echo $name | wc -m` $name; done
    4 Joe
    5 Jack
    5 John
    4 Doe
    5 Jill
    5 Hart
     
    My solution:
    $ while read LINE; do echo $(echo $LINE | wc -m) $LINE; done < names.txt
    4 Joe
    5 Jack
    9 John Doe
    10 Jill Hart
     
    My solution for listing and counting firstnames only and correcting the math to actually match the letter count of each name:
    $ while read LINE; do FNAME=$(echo $LINE | awk '{print $1}'); declare -i CLET; CLET=$(echo $FNAME | wc -m); CLET=$CLET-1; echo "$CLET $FNAME"; done < names.txt
    3 Joe
    4 Jack
    4 John
    4 Jill

    As others have said, people come for the comments. What I got out of the comments was the $() bit -- I too would have defaulted to backticks. My next step is to actually understand what the difference between backticks and $() is. Also to the sed and awk posters -- both are great.

    Starting Score:    1  point
    Karma-Bonus Modifier   +1  

    Total Score:   2  
  • (Score: 1) by b on Monday October 13 2014, @11:54PM

    by b (2121) on Monday October 13 2014, @11:54PM (#105763)
    $() allows nesting.

    Also, I'd quote the variables to work more elegantly with spaces (e.g. don't compress multiple spaces in a string). i.e. use "$LINE".

    FWIW, I usually use the -r flag for read, to prevent interpretation of backslashes.

    Finally (a minor point), you can use a heredoc instead of the extraneous echo.

    wc -m <<<"$LINE"

    • (Score: 2) by hemocyanin on Monday October 13 2014, @11:58PM

      by hemocyanin (186) on Monday October 13 2014, @11:58PM (#105766) Journal

      helpful info -- thank you

      • (Score: 1) by b on Tuesday October 14 2014, @12:36AM

        by b (2121) on Tuesday October 14 2014, @12:36AM (#105779)

        No worries. Much of it is fairly minor, for edge cases. Still, your code was a hell of a lot better than the tutorial!