Wednesday, July 27, 2011

Stdin, not stdout


Although using > and >> seems a relatively easy concept for most to pick up, it's common for others to have difficulties using the less-than symbols (< and <<). When thinking of > and >>, it's easiest to visualize them as the flow of output data moving from the command on the left to the destination file on the right. The same applies to < and <<. Using <, you essentially execute a command with stdin already supplied. Think of it as the data already provided supplied to the command on the left of the data as stdin (that is, <cmd> <- <data>).
For example, say you want to send an e-mail of an ASCII text file to another user. You could use a pipeline to redirect the stdout ofcat to stdin of mail (that is, cat mail_file.out | mail –s "Here's your E-mail!" acormany@yahoo.com), or you could redirect the contents of the file to become stdin for the mail command:
# mail –s "Here's your E-mail!" acormany@yahoo.com < mail_file.out


Using <<, also known as a here-document, can save some formatting time and is easier on the processing time of the command execution. By using <<, the string of text is directed to the command to execute as stdin, but you can continue to enter information until the termination identifier has been reached. Simply type the command following << and the termination identifier, type anything you want, and end it with the termination identifier on a new line. Using the here-document allows you to preserver whitespace, new lines, and so on.
For example, rather than typing five echo statements that UNIX would have to process individually:
# echo "Line 1"
Line 1

# echo "Line 2"
Line 2

# echo "Line 3"
Line 3

# echo "Line 4"
Line 4

# echo "Line 5"
Line 5


you could use the following code to replace the multi-echo statement, and UNIX would only need to process a single execution:
# cat << EOF
> Line 1
> Line 2
> Line 3
> Line 4
> Line 5
> EOF

Line 1
Line 2
Line 3
Line 4
Line 5


To allow tabs to make everything look a bit neater in the shell script, simply place a hyphen (-) between the << and the termination identifier:
# cat <<- ATC
> Line 1
> Line 2
> Line 3
> Line 4
> Line 5
> ATC

Line 1
Line 2
Line 3
Line 4
Line 5


Listing 7 provides an example of how to combine a few items discussed in this article so far.

Listing 7. Combining CLI

# cat redirect_example

#!/usr/bin/ksh

cat <<- ATC | sed "s/^/Redirect Example => /g" >> atc.out
This is an example of how to redirect
stdout to a file as well as pipe stdout into stdin
of another command (i.e. sed), all done inside
a here-document.

Cool eh?
ATC


Now let's see what the script looks like with the redirection and pipeline.
# ./redirect_example

# cat atc.out
Redirect Example => This is an example of how to redirect
Redirect Example => stdout to a file as well as pipe stdout into stdin
Redirect Example => of another command (i.e. sed), all done inside
Redirect Example => a here-document.
Redirect Example =>
Redirect Example => Cool eh?


No comments:

Post a Comment