DirectNET

Data Center Management Solutions including UPS Systems, Data Center Cooling, KVM over IP & IP Power Strips, Server Racks and Server Rack accessories; KVM Switches and KVM Extenders; Rackmount Monitors and Rackmount Keyboards.


NAVIGATION
Home
Store
INSIDE MAC
Television Shows
Broadcast Shows
Daily News Shows
Special Shows
EVENTS
DAILY TIPS
Design
Mac OS X
Mac OS X UNIX
COMMUNITY
Forums
Surveys
NEWS
Current
Press
Archive
FEATURES
Editorial
Dr. Mac
Reviews
Reader Reports
RESOURCES
FAQ
Documentation
Learning Center
MAN pages
Glossary
Tutorials
Tips
Links

OUR PARTNERS

OS X | UNIX

back

Unix

Mac OS X Unix Tutorial

by Adrian Mayo - Senior Editor for Mac OS X Unix, Janice Mayo - Senior Editor for Mac OS X Unix

Part 7 - Redirection and Pipelines (page 1 of 2)

The Story So Far

Parts 1 to 4 covered basic Unix theory. In Parts 5 and 6 we used our Unix skills to in a more practical way: displaying and locating files with commands such as 'head', 'tail', 'locate', and 'find'; searching and changing within files with commands 'grep' and 'sed'.

This part covers the concepts of re-direction and pipelines, and how one uses pipelines to combine simple commands to perform tasks that are more complex.

Parts 8 and 9 will cover shell scripting.

First, Part 6 finished with this 'sed' command to remove the HTML comment start and end markers we had previously added to test.html.

sed -e 's/<\!--//' -e 's/^ *<link rel=.*osxfaq\.css">/&/' -e 's/-->//' test2.html > test.html

How does it work?

Option '-e' introduces a regular expression. It is often used when one wishes to search for more than one regular expression at a time. Here, the search is for three: the previously added comment start marker; then the original text; then the added comment end marker. Markers are substituted with an empty string effectively removing them. The original text is replaced with itself (i.e. the matched pattern) using special character '&'.

Thus, the comment markers are removed and the file is restored. Simple huh! :-)


Input Output Streams

A Unix command receives its input from an input stream opened by the shell. Similarly, it writes its output to an output stream, again opened by the shell. The important point is that the command does not open input and output streams itself - it accepts them from the shell. The terms standard in (or stdin) and standard out (or stdout) are used to refer these streams.

Normally standard in is the keyboard and standard out is the Terminal window. Naturally, a command might open a file to read or write in preference to standard in or out, but many of the utility commands do work from the standard streams. This allows the shell, and ultimately yourself, to control from where a command reads its input, and to where it writes its output.

Try this.

% cat

Type a few lines of input. When 'return' is pressed the line just typed is echoed back. (Press control-d once when you get bored.)

What is happening? In this example an input file is not specified. 'cat' reads standard in (the keyboard) and writes each line to standard out (the Terminal window). Copy cat perhaps?

The command:

% cat letter.txt

writes the contents of 'letter.txt' to the Terminal window. This time 'cat' was given a filename to read in preference to standard in. It continues to write to standard out.

Redirecting Standard Out

The shell allows one to redirect standard in and standard out from/to a file.

The command:

% cat letter.txt > copy-of-letter.txt

effectively copies letter.txt to copy-of-letter.txt.

The '>' character redirects standard out to the specified file.

It is important to realise that as far as 'cat' is concerned:

% cat letter.txt
and
% cat letter.txt > copy-of-letter.txt

are the same.

The shell implements redirection. The shell opens/creates file 'copy-of-letter.txt' and passes this as standard out to 'cat'. Cat unknowingly writes to the file instead of the Terminal window.

Redirection works for all commands that write to the Terminal window. For example:

% ls -al > ls.txt

writes a directory listing to file 'ls.txt'.

Double Redirection

Double redirection occurs when one uses '>>' to redirect, instead of '>'. The only difference being the specified file is added to instead of being overwritten.

For:

% cat letter.txt > copy-of-letter.txt

the shell creates a new file called 'copy-of-letter.txt', and removes any existing file of that name.

For:

% cat letter.txt >> copy-of-letter.txt

the shell opens the existing file 'copy-of-letter.txt', and standard out is written to the end of the file leaving the original contents intact. If the file does not exist it is created.

Redirecting Standard In

Just like standard out, standard in can be redirected so that a file is read instead of the keyboard.

Redirecting standard in is not done as often as redirecting standard out. It is used mainly for commands that take no filenames and expect to use stdin and stdout. Such a command is 'tr'. The real value of having standard in is lies in 'piping' described later. The symbol to redirect standard in is '<'.

'tr' translates one character to another.

Try this

% tr 'p' 'a'

then type 'apple' followed by control-d.

A typical use for 'tr' is to translate files that have Mac-style end of line characters (carriage-return) into files that have Unix-style end of line characters (new-line). In this example, input is read from 'mac-file' and output is written to 'unix-file'. We must therefore redirect both standard in and standard out, as 'tr' takes no filenames.

The special character '\r' is used by 'tr' to represent carriage-return.
The special character '\n' is used by 'tr' to represent new-line.

The command is this.

% tr '\r' '\n' < mac-file > unix-file

Take 'mac-file', translate each carriage-return to line-feed, and write the result to 'unix-file'.

Note: because 'tr' processes a line at a time, it is not possible to write back to the file being read. If you wish to do this use the following trick:

tr '\r' '\n' < file > tmp; mv tmp file

The ';' character separates two commands on a single line, and is often used when forming an alias that executes many commands.

 
Tell Me More...

cat

Remember from a previous tutorial that the 'cat' command is designed to concatenate files. It takes a list of files as input and joins them together by sending them one after another to the standard output.

Question:

What is the difference between:

% cat letter.txt

and

% cat < letter.txt

Answer:

To the user, none.

To the shell and 'cat' this...

-> cat letter.txt

'cat' opens the file letter.txt and reads from it. letter.txt is not made the standard input. Standard input is still the keyboard but 'cat' ignores it.

-> cat < letter.txt

'cat' sees no input file and therefore reads standard in.

It is the shell that interprets:

'< letter.txt'

opening file 'letter.txt' and passing it as standard in (instead of the keyboard) to 'cat'.

Standard Error

Unix commands write error and warning messages as well as regular output. Often it is desirable to separate the two, especially in shell scripts because regular output is processed, but error messages are written to the Terminal window.

To enable this a command actually has two output streams available, standard out and standard error (or stderr). Normally both of these are set by the shell to be the Terminal window.

Redirect Standard Out and Error

Redirecting standard out does not redirect standard error too.

Try this:

% cat zxzxz

You will get an error message (assuming 'zxzxzx' does not exist).

% cat zxzxz > out

will create an empty 'out' file and the error message still appears on the terminal.

To redirect standard out and standard error use:

% cat zxzxz >& out

To redirect them to different files use the following trick:

% ( cat letter.txt > out ) >& err

(All one one line).

This works by creating a subcommand that redirects standard out to 'out', then the output of the subcommand (now only standard error) is in turn redirected to 'err'.


Next Page

Next page I will introduce 'pipelines'. Piping allows one to join commands together by directing the output of the first command to the input of the following command.


previous

Part 7 - Redirection and Pipelines (page 1 of 2)

next

Copyright © 2000-2008 Inside Mac Media, Inc. All rights reserved.
Apple assumes no responsibility with regard to the selection, performance, or use of the products or services. All understandings, agreements, or warranties, if any, take place directly between the vendors and prospective users.
Apple, the Apple logo, Mac, PowerMac G4, PowerMac G5, Xserve, Xserve RAID, PowerBook, iBook, Airport, AirPort Extreme, iMac, eMac, iLife, iMovie, iCal, iPhoto, iTunes, QuickTime, FireWire, iPod, iSight, AppleWorks, Macintosh, Jaguar, Panther, Mac OS, Mac OS X and Mac OS X Server are trademarks of Apple Computer, Inc.