05.01.2013 Views

Mac OS X Leopard - ARCAism

Mac OS X Leopard - ARCAism

Mac OS X Leopard - ARCAism

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

One final common conditional is to utilize the exit status of a command. Every command<br />

you run will provide an exit status; that’s usually a 0 if everything went smoothly and some other<br />

number if something went wrong. if knows about this, so you can use statements like this:<br />

if command<br />

then<br />

if command exits normally do this<br />

else<br />

if command resulted in an error do this<br />

fi<br />

Another way of dealing with multiple potential conditions is the use of the case statement.<br />

A case statement looks at a variable and responds differently depending on its value.<br />

A case statement is generally much easier to use then an if statement; however, it’s a viable<br />

alternate only if you are dealing with multiple similar outcomes that may be stored in a singe<br />

variable. A case statement looks like this:<br />

Loops<br />

case "$caseVar" in<br />

1) command(s) to run if the value of $caseVar is "1" ;;<br />

2) command(s) to run if the value of $caseVar is "2" ;;<br />

dog) command(s) to run if the value of $caseVar is "dog" ;;<br />

*) command(s) to run if none of the previous cases matched, "*" is a wildcard ;;<br />

esac<br />

Now that you’ve learned a bit about how to selectively run or not run a command based on a<br />

condition, we will cover how you can run a command over and over with loops. There are three<br />

main types of loops for shell scripting: while, until, and for loops.<br />

The while and until loops are similar in idea, but they do the opposite in practice. Each of<br />

them takes a condition (similar to the if statement); however, where the while loop will run a<br />

block of code as long as the condition is true, the until loop will run as long as the condition is<br />

false.<br />

CAUTION Poorly written loops can have the adverse effect of running forever. If you are running<br />

a script from the command line, it’s easy enough to stop such a runaway script with<br />

Ctrl+C, but if this is part of a background or start-up script, things can get more complicated.<br />

Therefore, it’s a good idea to test your scripts from the command line before you place them<br />

in start-up files and such.<br />

A simple while loop could look like this:<br />

#!/bin/sh<br />

x=1<br />

while [ $x -le 10 ]<br />

do<br />

echo $x<br />

x=`expr $x + 1`<br />

done<br />

CHAPTER 19 EXTENDING THE POWER OF DARWIN 335<br />

This script will simply go through the while loop, printing the value of $x and then increasing<br />

the value of x by 1 as long as the value of $x is equal to or less than 10. If we switched while<br />

to until, the loop would skip entirely since our declared value of x (1) would immediately be less<br />

than or equal to 10.<br />

NOTE Like if statements, while and until loops can also evaluate multiple conditions using<br />

logical and (&&) and or (||) statements.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!