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!!!
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
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"
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?)
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
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) |