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.

338<br />

CHAPTER 19 EXTENDING THE POWER OF DARWIN<br />

If our counter is set up properly (and there aren’t any other glitches in our script), line 15<br />

will run on the last of our input values, also printing it surrounded by quotes, but then instead of<br />

placing a comma (as if to continue the list), it will print out a . and then a newline character.<br />

Line 16 then ends the if statement, and line 17 ends the loop and thus our little script.<br />

One thing not shown here is that the read command can actually assign different variables<br />

to the input, where, for example, each input value would be assigned its own variable . . . well,<br />

that is if we assume that the number of variables and the number of words are the same. If we<br />

had the following read statement:<br />

read a b c<br />

and entered this:<br />

one two three<br />

then $a would be assigned the value one, $b would be assigned the value two, and $c would be<br />

assigned the value three. In the event we set read to accept three variables and we get four or<br />

more input terms, the last read variable will absorb all the remaining values.<br />

TIP If the integrity of your read variables is important and you want to protect against additional<br />

input, you could assign an additional read variable to suck up all the extra data. So if you<br />

really wanted only three input values, you could set up read with four variables and then just<br />

use the first three, thus essentially ignoring all the excess input.<br />

One final useful related tidbit is the ability to merge an entire file into a script. This is done<br />

quite easily with the dot (that is, period) command. If you paid attention, you saw this in our<br />

sample .bash_profile file toward the end of the previous chapter where we used this:<br />

if [ -f ~/.bashrc ]; then<br />

. ~/.bashrc<br />

fi<br />

Here we first check to see whether our desired file (.bashrc) exists and is a normal file. If so,<br />

we include it with the . ~/.bashrc. And that’s it. This is a handy way to create complex scripts<br />

where you can write entire parts as separate files and then include them all together to effectively<br />

run as one.<br />

CAUTION If you do combine scripts together, be careful with your variables. If two different<br />

script files use the same variable names and are then included together, unexpected (often<br />

bad) things can happen.<br />

Advanced Scripting with Perl, Python,<br />

and Ruby<br />

Shell scripting is very useful and fairly powerful for many things; however, there are a number of<br />

more powerful languages for writing scripts than the shell. <strong>Leopard</strong> comes with three of the<br />

biggest and most powerful scripting languages in use today: Perl, Python, and Ruby. Given the<br />

breadth of these languages (which are often referred to as interpreted programming languages or<br />

very high-level languages [VHLLs], rather than just scripting languages), it would be impossible<br />

to properly teach you how to use them in all their glory in this chapter. Although these languages<br />

are fairly easy to learn to use (some perhaps more so than others), the space (and time) needed to

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

Saved successfully!

Ooh no, something went wrong!