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.

308<br />

CHAPTER 18 INTRODUCING DARWIN AND THE SHELL<br />

Pipes<br />

Piping one command into another is a great way to make even the simplest Darwin tools do<br />

powerful things. We saw this previously when we piped the ps command into the less command.<br />

The pipe symbol is the | (which is the tall line that lives above the \ on a normal US <strong>Mac</strong> keyboard).<br />

In practice, this takes the first command and sends the output into the second.<br />

Commands like less rely almost entirely on the ability to pipe one command into it, and other<br />

commands become much more useful with this ability. For example, the ability to take commands<br />

that produce large amounts of output and pipe that content into a filter (like the grep<br />

command) can save lots of time and headaches.<br />

Redirects<br />

A redirect allows you to alter what happens to the output of a command, or alternately direct the<br />

content of a file into a command. The symbols for redirection are < and >. A very simple use of a<br />

redirect is to create a text file using echo, like this:<br />

<strong>Leopard</strong>:~ scott$ echo "Hello my name is Scott" > name.txt<br />

<strong>Leopard</strong>:~ scott$ cat name.txt<br />

Hello my name is Scott<br />

The echo command normally would just print out whatever you feed into it back to your terminal,<br />

but here we redirected the output to name.txt (which may or may not have existed).<br />

CAUTION If you are redirecting data into an existing file, the entire contents of that file will<br />

be replaced with the new data. So be very careful with this command.<br />

If you wanted to redirect additional data into an existing file (rather than replace the content,<br />

which the > always does), >> can be used to append the new data to the old:<br />

<strong>Leopard</strong>:~ scott$ echo " Hello Scott" >> name.txt<br />

<strong>Leopard</strong>:~ scott$ cat name.txt<br />

Hello my name is Scott<br />

Hello Scott<br />

<strong>Leopard</strong>:~ scott$ echo "Ooops" > name.txt<br />

<strong>Leopard</strong>:~ scott$ cat name.txt<br />

Ooops<br />

Background Tasks<br />

Any Darwin command can be issued to run in the background with the & symbol tacked onto the<br />

end of the command. This is particularly useful when you want to start a command that may<br />

take a long time to finish, or when running a task that you want to keep running indefinitely. For<br />

example, if we wanted to use the find command to find something with the name motd somewhere<br />

on our system, knowing that this may take some time, we may want to run it in the<br />

background. Here’s an annotated example of this:<br />

<strong>Leopard</strong>:~ scott$ find / -name "motd" > found 2> found_err &<br />

[1] 358<br />

Here we start our find command in the background. We are redirecting our output to a file<br />

named found. Also, the 2> found_err will redirect any error messages to a file named found_err<br />

(otherwise, even though the command is running in the background, error messages would still<br />

spam our terminal).<br />

<strong>Leopard</strong>:~ scott$ jobs<br />

[1]+ Running find / -name "motd" >found 2>found_err &

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

Saved successfully!

Ooh no, something went wrong!