IF the variable $? is equal to 0, THEN print out a message. Otherwise (else), print out a different message. FYI, "$?" checks the exit status of the last command run.if [ $? -eq 0 ] ; then print we are okay else print something failed fi
The final 'fi' is required. This is to allow you to group
multiple things together. You can have multiple things between
if and else, or between
else and fi, or both.
You can even skip the 'else' altogether, if you dont need an alternate case.
if [ $? -eq 0 ] ; then print we are okay print We can do as much as we like here fi
While the syntax is similar to C on the surface, there are some major differences;
echo input yes or no read answer case $answer in yes|Yes|y) echo got a positive answer # the following ';;' is mandatory for every set # of comparative xxx) that you do ;; no) echo got a 'no' ;; q*|Q*) #assume the user wants to quit exit ;; *) echo This is the default clause. we are not sure why or echo what someone would be typing, but we could take echo action on it here ;; esac
There are two ways to stop the loop. The obvious way is when the 'something' is no longer true. The other way is with a 'break' command.
keeplooping=1; while [[ $keeplooping -eq 1 ]] ; do read quitnow if [[ "$quitnow" = "yes" ]] ; then keeplooping=0 fi if [[ "$quitnow" = "q" ]] ; then break; fi done
until [[ $stopnow -eq 1 ]] ; do echo just run this once stopnow=1; echo we should not be here again. done
The basic syntax is
Whatever name you put in place of 'var', will be updated by each value following "in". So the above loop will print outfor var in one two three ; do echo $var done
But you can also have variables defining the item list. They will be checked ONLY ONCE, when you start the loop.one two three
The two things to note are:list="one two three" for var in $list ; do echo $var # Note: Changing this does NOT affect the loop items list="nolist" done