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.

The first thing you may notice is that unlike the echo command, printf does not automatically<br />

add a linefeed to each statement. By using printf in line 3, the prompt for the read<br />

statement appears on the some line rather than the line below.<br />

The read statement is on line 4, and it’s fairly easy: the read command followed by a variable<br />

to contain our input (cleverly called input).<br />

In line 5 we set the input as our primary script arguments.<br />

Line 6 starts a counter and starts it with $#, which is a special variable that tells the total<br />

number of arguments assigned by set in line 5.<br />

Line 7 provides some text for our output.<br />

Line 8 begins a for loop that will iterate through each of the arguments set in line 5. The $@<br />

is another one of those special variables that contains all of our arguments in a single variable. In<br />

this case, $@ and $input will contain the same values.<br />

Line 9 starts the for loop.<br />

Line 10 decreases the value of our counter by 1.<br />

Line 11 uses an if statement to see whether our counter is greater than 0. The counter is set<br />

up so that when it hits 0, the loop will be on our final input argument, so we continue the loop<br />

on line 13 until we reach the last of our inputs and then switch over to line 15.<br />

Line 13 uses the printf statement to print one of our arguments surrounded by quotes. To<br />

get the quote to print, rather than get interpreted as the closing or opening of the text we are<br />

printing, we need to escape the ". We do this by preceding the quote symbol with a backslash (\)<br />

. printf knows a number of these formatting escape characters (which are actually borrowed<br />

from the C programming language) and will automatically expand them into their proper form<br />

when printing. Table 19-3 lists the common escape characters.<br />

Table 19-3. Escape Character Expansion<br />

Escape Character Name Output Result<br />

\a Alert (bell) Activates the terminals bell. Depending on your Terminal<br />

preferences (set on the Advanced tab of the Terminal<br />

Inspector), this will either flash the terminal screen or make<br />

an audible noise (or both or neither). By default the terminal<br />

window will flash.<br />

\b Backspace Moves the cursor back one space.<br />

\f Form feed Clears the screen. Traditionally this would eject the current<br />

sheet of paper and start a new sheet.<br />

\n Newline Moves the input to the beginning of the next line, like hitting<br />

Return.<br />

\r Carriage return Moves the cursor back to the beginning of the current line;<br />

any further input would overwrite what’s already there.<br />

\t Tab Moves the cursor one tab space to the right.<br />

\v Vertical tab Moves the cursor down one line; however, unlike \n, it will<br />

not return to the beginning of the line.<br />

\\ Backslash Prints the backslash character.<br />

\' Single quote Prints a single quote character.<br />

\" Double quote Prints a double quote character.<br />

CHAPTER 19 EXTENDING THE POWER OF DARWIN 337

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

Saved successfully!

Ooh no, something went wrong!