Bash scripting 101: Chapter 5: WHILE we are at it and UNTIL we are done (more loopy loops)





help while at the bash promt gives

while: while COMMANDS; do COMMANDS; done
    Expand and execute COMMANDS as long as the final command in the
    `while' COMMANDS has an exit status of zero.

not very informative?

actually it is (and if you notice how i'm now trying to make you use the built in help system? then you are right! and yes it's cheating kinda. I'm actually trying to teach you how to get the info yourself, not depend on me to do it for ya)

remember the IF and FOR syntax? this is kinda a mix, as long as the commands after while are "true"/"exit status zero" do the commands

so how can i use it then???

what good is it?

ahhh!!! here goes

#!/bin/bash

#Set COUNTER variable to 0
COUNTER=0

# test if COUNTER is -ne (NOT EQUAL arithmetic (mathematic)) to 50
while test $COUNTER -ne 50
do
   echo -e "iteration of script: $COUNTER\n"
   # use the old "let VAR=$VAR+1"
   # as opposed to newer "let $COUNTER++" for backwards compatability
   let COUNTER=$COUNTER+1
done
exit 0

wow that's about the least new stuff we've had in a script so far!!!

  1. #!/bin/bash
    shebang line, (old news)

  2. COUNTER=0
    make the variable COUNTER be 0

  3. while test $COUNTER -ne 50
    each time you run the below block, check(test) if the variable COUNTER is _not equal_ to 50 arithmetically, -ne is arithmetically (mathematically), if it IS not equal to 50, then do the "do/done" block below again

  4. do
    start the do/done block for while

  5. echo -e "iteration of script: $COUNTER\n"
    print which iteration (repeat) of the while it is

  6. let COUNTER=$COUNTER+1
    add one to COUNTER variable (use the old way..see the # (comments) on why)

  7. done
    end the do/done block

okay..so it counts from 0-49, big deal!!!

nope not really it's not..but it can be used to be more clever, you may want to do something for each line in a file, in which case filtered "wc filename" might be used as a variable and fed to while

or say you want to use in a script something to read multiple lines, sure ya can use cat (here now files) but you can also do something like below

#!/bin/bash
# this function asks if you want an extra copy created with .bak extension
function GETOPTION
{
echo -e "do you want 2 copies created from these entries? (y/n)?\n"
read -e BACKUP
}
GETOPTION

# not going to check for y/n more than once, if a user can't read they deserve some problems
if test "$BACKUP" == ""
then
echo -e "you need to answer \"y\" or \"n\" to this question\n"
GETOPTION
fi

until test "$TEXT_ENTRY" = "THE_END"
do
read -e TEXT_ENTRY
echo -e "$TEXT_ENTRY<P>\n" >> testtxt.txt
if test "$BACKUP" = "y" ; then
echo -e "$TEXT_ENTRY<P>\n" >> testtxt.txt.bak
fi
done
exit 0

  1. #!/bin/bash
    shebang line, (old news)

  2. function GETOPTION
    in the { } area below..is contained the function (subroutine) "option"

  3. {
    beginning of the function content

  4. echo -e "do you want 2 copies created from these entries? (y/n)?\n"
    (echo is pretty old news by now right?)

  5. read -e BACKUP
    read...(old news see chapter 2)

  6. }
    end the function GETOPTION

  7. GETOPTION
    call the function GETOPTION(execute it)

  8. if test "$BACKUP" == ""
    if test "$BACKUP" is equal to nothing

  9. then
    then... (c'mon "if/then/elif/fi" are old news now)

  10. echo -e "you need to answer \"y\" or \"n\" to this question\n"
    echo the error message cuz you didn't respond to the question

  11. GETOPTION
    call the function GETOPTION once more to give you one more chance if you didn't enter y or n

  12. fi
    end the if block

  13. until test "$TEXT_ENTRY" = "THE_END"
    WHOOAAAA!!! what's this??? well "until" is pretty much a negated "while"(not quite in all cases) but in most

    as long as the variable TEXT_ENTRY contains something else than THE_END, keep this loop running

    yes, it could probably have been done while test ! "$TEXT_ENTRY" = "THE_END"

  14. do
    i don't _really_ need to explain do/done blocks by now do i?

  15. read -e TEXT_ENTRY
    read into variable TEXT_ENTRY, do this everytime the do/done block is run

  16. echo -e "$TEXT_ENTRY<P>\n" >> testtxt.txt
    echo the contents of TEXT_ENTRY variable, add a html p tag (paragraph) and a linebreak at the end, and dump it (adding to the file) into testtxt.txt in the current directory

  17. if test "$BACKUP" = "y" ; then
    ok i'm grown a little lazy and starting to do it a bit more like my normal style

    everything here cept "; then" should be clear, it's the same as

    if test "$BACKUP" = "y"
    then

    i'm just doing it on one line is all, and using separator ";" (remember that one from chapter one?)

  18. echo -e "$TEXT_ENTRY<P>\n" >> testtxt.txt.bak
    if you specified BACKUP ( y ) then dump the same variable in the same way to another file

  19. fi
    end the if block

  20. done
    end the until/do/done block

  21. exit 0
    exit with successful (c'mon this one is so old i'm not even gonna bother with it in the future..just use it, or not..i suggest you do, it'll help you further on

    If you paid lots of attention you probably caught the other "exit" that should be in this script.

    NO? go back and search again..one more place in this script deserves an "exit 0", actually it deserves a "return 0", so what is the differance? exit exits the shell with status given, return exits a subroutine (function) durn..i gave it away didn't i? ahh one freebie you can have..tis on the house

here is a while && until "workbench" file you can experiment with at home

so go experiment and have some fun :-))

two programs you might want to look at while experimenting are "true" and "false" they can be useful every now and then for shell scripting, do a "man" on them (or a "help", most bash shells today have "true" and "false" built-in)




Bash scripting 101: Chapter 4: IF he's jolly why is he loopy? (loopy loops) bash scripting 101: Chapter 6: can you html (putting some things together)



w0nderer