Professional Documents
Culture Documents
iii
Chapter 1. Standard In and Standard Out
Key Concepts
• Terminal based programs tend to read information from one source, and write information to one
destination.
• The source programs read from is referred to as Standard In (stdin), and is usually connected to a
terminal’s keyboard.
• The destination programs write to is referred to as Standard Out (stdout), and is usually connected to a
terminal’s display.
• When using the bash shell, stdout can be redirected using > or >>, and stdin can be redirected using <.
Discussion
Many Linux commands read input from the keyboard and display output to the terminal. In this
Workbook, you’ll learn how you can redirect where input is read from and where output goes. The
output of one command can be used as the input for another command, allowing simple commands to be
used together to perform more complicated tasks.
Graphical Programs
Graphical programs are designed to run in the X graphical environment. They expect the user to be
using a mouse, and use common graphical components, such as popup menus and buttons, for user
input. The mozilla web browser is an example of a graphical program.
Screen Programs
Screen based programs expect to use a text console. They make use of the entire display, and handle
text placement and screen redraws in sophisticated ways. They do not require a mouse, and are
appropriate for terminals and virtual consoles. The vi and nano text editors, and links web browser,
are examples of screen based programs.
Terminal Programs
Terminal programs collect input and display output in a stream, seldom if ever redrawing the screen,
as if writing directly to a printer that does not allow the cursor to move back up the page. Because of
their simplicity, terminal based programs are often called simply commands. ls, grep, and useradd
are examples of terminal based programs.
4
Chapter 1. Standard In and Standard Out
This chapter focuses on the latter type of program. Do not let the simplicity of the way these commands
receive input and output fool you. You will find that many of these commands are very sophisticated, and
allow you to use the command line interface in powerful ways.
Redirecting stdout
Notice that no output is displayed to the terminal. The ps command writes to stdout, as it always does,
but stdout is redirected by the bash shell to the file sluggish.txt. The user elvis can examine the file
later, at a more convenient time.
rha030-5.0-2-en-2007-10-16T13:03:19-0400 5
Copyright (c) 2003-2007 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other
use is a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise
duplicated whether in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or
otherwise improperly distributed please email training@redhat.com or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.
Chapter 1. Standard In and Standard Out
Redirecting stdin
Just as bash uses > to coax commands into delivering their output somewhere other than the display,
bash uses < to cause them to read input from somewhere other than the keyboard. The user elvis is still
trying to figure out why his machine was acting sluggish. He talked to his local system administrator,
who thought that looking at the list of currently running processes sounded like a good idea, and asked
elvis to mail him a copy.
Using the terminal based mail command, elvis first writes an email message to the administrator
"manually", from the keyboard. The mail command expects a recipient as an argument, and the subject
line can be specified with the -s command line switch. The email body is then entered from the keyboard.
The end of the message text is signaled by a lone period on a line.
Hey sysadmin...
I’m sending a list of processes that were running when the computer was running
in a separate email.
Thanks! --elvis
.
Cc:
For his follow-up message, elvis can easily mail the output of the ps command he recorded in the file
sluggish.txt. He just redirects the mail command’s stdin stream to be read from the file.
rha030-5.0-2-en-2007-10-16T13:03:19-0400 6
Copyright (c) 2003-2007 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other
use is a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise
duplicated whether in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or
otherwise improperly distributed please email training@redhat.com or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.
Chapter 1. Standard In and Standard Out
The system administrator will receive an email from elvis, with "ps output" as it’s subject line, and the
contents of the file sluggish.txt as its body.
In the first case, the mail process’s stdin was connected to the terminal, and the message body was
provided by the keyboard. In the second case, bash arranged for the mail process’s stdin to be connected
to the file sluggish.txt, and the message body was provided by its contents. The mail command
doesn’t change its basic behavior: It reads the body of the email message from stdin. 1
Using the ps command to look up the process’s PID, elvis next examines the process’s /proc/pid /fd
directory.
[elvis@station elvis]$ ps
PID TTY TIME CMD
1368 pts/1 00:00:00 bash
1910 pts/1 00:00:00 cat ➊
1911 pts/1 00:00:00 ps
[elvis@station elvis]$ ls -l /proc/1910/fd ➋
total 0
lrwx------ 1 elvis elvis 64 Sep 13 06:42 0 -> /dev/tty1
lrwx------ 1 elvis elvis 64 Sep 13 06:42 1 -> /dev/tty1
lrwx------ 1 elvis elvis 64 Sep 13 06:42 2 -> /dev/tty1
lr-x------ 1 elvis elvis 64 Sep 13 06:42 3 -> /etc/termcap
rha030-5.0-2-en-2007-10-16T13:03:19-0400 7
Copyright (c) 2003-2007 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other
use is a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise
duplicated whether in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or
otherwise improperly distributed please email training@redhat.com or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.
Chapter 1. Standard In and Standard Out
Not surprisingly, the cat process has the file /etc/termcap open (it must be able to read the file to
display its contents). Perhaps a little surprising, it is not the only, or even the first, file that the process has
open. The cat command has three open files before it, or, more exactly, the same file open three times:
/dev/tty1.
As a Linux (and Unix) convention, every process inherits three open files upon startup. The first, file
descriptor 0, is Standard In. The second, file descriptor 1, is Standard Out, and the third, file descriptor 2,
is Standard Error (to be discussed in the next Lesson). What open files did the cat command inherit from
the bash shell that started it? The device node /dev/tty1 for all three.
Table 1-1. Standard In, Standard Out, and Standard Error File Descriptors
Recall that /dev/tty1 is the device node which connects to the console serial driver within the kernel.
Whatever elvis types can be read from this file, and whatever is written to this file is displayed on elvis’s
terminal. What happens if the cat process reads from stdin? It reads input from elvis’s keyboard. What
happens if it writes to stdout? Whatever is written is displayed on elvis’s terminal.
Redirection
In the next example, elvis cat’s the /etc/termcap file, but this time redirects stdout to the file
/tmp/foo. Again, elvis suspends the command in mid-stride with the CTRL-Z control sequence.
Using the same technique as above, elvis examines the files opened by the cat command, and the file
descriptors associated with them.
[elvis@station elvis]$ ps
PID TTY TIME CMD
1368 pts/1 00:00:00 bash
1976 pts/1 00:00:00 cat
1977 pts/1 00:00:00 ps
[elvis@station elvis]$ ls -l /proc/1976/fd
total 0
lrwx------ 1 elvis elvis 64 Sep 13 07:05 0 -> /dev/pts/1
l-wx------ 1 elvis elvis 64 Sep 13 07:05 1 -> /tmp/foo ➊
lrwx------ 1 elvis elvis 64 Sep 13 07:05 2 -> /dev/pts/1
lr-x------ 1 elvis elvis 64 Sep 13 07:05 3 -> /etc/termcap
➊ Notice that file descriptor 1 (in other words, Standard Out) is no not connected to the terminal, but
instead to the file /tmp/foo.
rha030-5.0-2-en-2007-10-16T13:03:19-0400 8
Copyright (c) 2003-2007 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use is
a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whether
in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributed
please email training@redhat.com or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.
Chapter 1. Standard In and Standard Out
What happens when elvis redirects both Standard Out and Standard In?
➊ File descriptor 0 (Standard In) is not connected to the terminal, but instead to the file
/etc/termcap.
When the cat command is called without arguments (i.e., without any filenames of files to display), it
displays Standard In instead. Rather than opening a specified file (using file descriptor 3, as above), the
cat command reads from stdin instead.
What is the effective difference between the following three commands?
There is none. In order to appreciate the real benefit of designing commands to read from Standard In in
lieu of named files, we must wait until pipes are introduced in a subsequent Lesson.
Examples
The following examples include a quick example of how new users can often get confused by commands
that read from Standard In, and a couple of more "real world" examples that use the ftp and gnuplot
programs. The ftp and gnuplot programs are both complicated programs, and these examples merely
introduce enough of their functionality to emphasize one of the main themes of this Workbook: if the
program is driven from a command line interface, it can usually be automated with a simple text script
and redirection.
rha030-5.0-2-en-2007-10-16T13:03:19-0400 9
Copyright (c) 2003-2007 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use is
a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whether
in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributed
please email training@redhat.com or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.
Chapter 1. Standard In and Standard Out
ape
giraffe
fish
[blondie@station blondie]$ sort zoo
ape
elephant
fish
giraffe
seal
As the name of the command suggests, the sort command (in its simplest form) reads a file, and writes
the contents sorted line by line alphabetically. Like the cat command, the sort command, when not
provided any arguments (i.e., filenames of files to sort), the sort command will look to stdin for its input.
While this behavior seems (and is) perfectly reasonable, it often confuses new users who innocently type
a command’s name, "just to see what it does". In the following, assume that blondie does not yet know
about Standard In. Exploring, she invokes the sort command. Not understanding that the sort command
is waiting to read Standard In, i.e., her keyboard, she tries to somehow exit the command she’s started.
Finally, a friend whispers to her, "CTRL-D".
Upon typing CTRL-D, the conventional "End of File" control sequence (recall Workbook 1), the sort
command prints a sorted list of everything it read on Standard In.
rha030-5.0-2-en-2007-10-16T13:03:19-0400 10
Copyright (c) 2003-2007 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use is
a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whether
in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributed
please email training@redhat.com or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.
Chapter 1. Standard In and Standard Out
When the ftp command pauses with the ftp> prompt, blondie types commands to navigate the ftp
server’s directories. If blondie downloaded this file often, she might be tempted to write a simple text file,
getreadme.ftp, that would reproduce the commands she typed from the keyboard. She could then run
the same command, ftp ftp.kernel.org. This time, however, she would use < to cause bash to redirect
stdin from the file getreadme.ftp. When the ftp command reads input from its stdin stream, the
information is provided by the file instead of the keyboard.
First, blondie uses a simple text editor to create the file getreadme.ftp containing all of the commands
that she typed interactively on the keyboard (including the password she supplied to the anonymous ftp
server, blondie@example.com).
rha030-5.0-2-en-2007-10-16T13:03:19-0400 11
Copyright (c) 2003-2007 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use is
a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whether
in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributed
please email training@redhat.com or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.
Chapter 1. Standard In and Standard Out
get README
quit
Notice how the contents of the file match exactly what she typed when using the ftp command above.
Next, she reruns the ftp ftp.kernel.org, but redirects stdin from the newly created file.
After the command has run, blondie has a new README file in her directory, which was downloaded by
the ftp command. blondie did encounter a couple of hitches, however.
• First, the command paused, and she needed to hit RETURN once to get it to complete. For security
reasons, many commands, when reading passwords, do not read the passwords from stdin, but from
the terminal directly. (Commands do not have to rely on stdin as their sole source of input, but most
choose to do so.) When ftp attempted to read the password from the terminal, the program suspended
until blondie hit the RETURN key.
• Secondly, there is an odd line specifying ?Invalid Input. Because the password was read from the
terminal directly, it was not consumed from getreadme.ftp file. When the ftp command went to
read its next line of input, it read blondie@example.com, which it reasonably didn’t recognize as a
command.
• Lastly, directory listings were dumped to the terminal when the command was run. When the ftp
command ran the ls commands from getreadme.txt, it wrote the output to stdout, which is still
connected to the terminal. Since blondie knows where the file is located, and has embedded that
information into the script, she does not need to see these listings every time she runs the command.
To address these issues, she first takes advantage of a ~/.netrc file. The ftp command is designed to
look for such a file in the user’s home directory, and if it exists, use it to provide the user’s username and
password. After examining the netrc(5) man page, blondie uses a simple text editor to create the
following ~/.netrc file.
Because the ~/.netrc file will now provide her username and password, she removes them from her
getreadme.ftp script. Secondly, she removes the unnecessary ls commands from the script as well.
rha030-5.0-2-en-2007-10-16T13:03:19-0400 12
Copyright (c) 2003-2007 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use is
a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whether
in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributed
please email training@redhat.com or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.
Chapter 1. Standard In and Standard Out
Armed with her ~/.netrc file (to provide her username and password) and her modified
getreadme.txt (to provide the commands for the ftp program), she reruns the ftp command, and the
operation runs smoothly.
A little frustrated that the two lines of text headers will interfere with the plotting of the data, madonna
opens the file stats.txt in a text editor and easily deletes them.
rha030-5.0-2-en-2007-10-16T13:03:19-0400 13
Copyright (c) 2003-2007 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other
use is a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise
duplicated whether in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or
otherwise improperly distributed please email training@redhat.com or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.
Chapter 1. Standard In and Standard Out
To plot the data, she uses gnuplot, a sophisticated plotting package which uses commands read from a
terminal interface to generate plots of mathematical functions and numeric data. After some browsing
through the online help available within gnuplot, she develops the following commands to plot her data
as a PNG graphics file called cpu.png.
G N U P L O T
Version 3.7 patchlevel 3
...
After quiting gnuplot, she returns to the bash shell, where she uses the eog image viewer to view her
plot.
rha030-5.0-2-en-2007-10-16T13:03:19-0400 14
Copyright (c) 2003-2007 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other
use is a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise
duplicated whether in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or
otherwise improperly distributed please email training@redhat.com or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.
Chapter 1. Standard In and Standard Out
Because madonna thinks she will want to generate a similar plot often, and doesn’t go through the agony
of typing in gnuplot’s plot command every time, she generates a script which can be used to automate
gnuplot. Using a text editor, she creates the file cpu_plot.gnuplot, which contains all of the gnuplot
commands she entered from the keyboard, paying close attention to put one command per line.
Now she can easily plot newly collected data by redirecting her script as gnuplot’s stdin.
Online Exercises
Lab Exercise
Objective: Use bash shell redirection to effectively control Standard In and
Standard Out.
Estimated Time: 20 mins.
Specification
1. The hostname command reports your station’s currently assigned hostname. Run the command
(without arguments), and redirect the output to the file ~/stdoutlab.txt.
2. The uptime command reports how much time has passed since your machine was booted, and other
system utilization information. Run the uptime command (without arguments), using redirection to
append the output to the file ~/stdoutlab.txt.
3. The uname -a command lists information about your current kernel version. Run the command,
using redirection to append the output to the file ~/stdoutlab.txt.
If you have completed the previous three steps successfully, you should be able to reproduce output
similar to the following. (Do not be concerned if your actual information differs from that shown
below).
[student@station student]$ cat stdoutlab.txt
station
07:09:31 up 11:30, 5 users, load average: 0.19, 0.06, 0.01
Linux station 2.4.20-20.9 #1 Mon Aug 18 11:45:58 EDT 2003 i686 i686 i386 GNU/Linux
rha030-5.0-2-en-2007-10-16T13:03:19-0400
Copyright (c) 2003-2007 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other
15
use is a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise
duplicated whether in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used,
copied, or otherwise improperly distributed please email training@redhat.com or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.
Chapter 1. Standard In and Standard Out
4. Generate a simple text file, ~/script.gnuplot, that will act as script to drive gnuplot. When
gnuplot reads your script from stdin, it should generate a plot of a simple mathematical expression,
such as the sine of x (sin(x)), or x squared (x**2). The plot should be generated as a PNG
graphics file called "gnuplot.png".
Once completed, your script should be able to be used as in the following example.
[student@station student]$ ls
script.gnuplot
[student@station student]$ gnuplot < script.gnuplot
[student@station student]$ ls
gnuplot.png script.gnuplot
[student@station student]$ file gnuplot.png
gnuplot.png: PNG image data, 640 x 480, 8-bit colormap, non-interlaced
[student@station student]$ eog gnuplot.png
Deliverables
1. A file called ~/stdoutlab.txt, which contains the output of the hostname command, followed by the output
of the uptime command, followed by the output of the uname -a command.
2. A script ~/script.gnuplot, which when used as stdin for the gnuplot command, generates a PNG graphics
file titled gnuplot.png containing a plot of a simple mathematical function.
Once you can produce graphs, set your terminal type to png (for PNG graphics), and your output file to
"gnuplot.png", using the following two commands ...
... and generate your plot again. Once you have figured out the sequence of commands to generate a plot
as a PNG file, record the commands as your gnuplot script.
rha030-5.0-2-en-2007-10-16T13:03:19-0400 16
Copyright (c) 2003-2007 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use is a violation of U.S.
and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whether in electronic or print format without
prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributed please email training@redhat.com or phone toll-
free (USA) +1 866 626 2994 or +1 (919) 754 3700.
Chapter 1. Standard In and Standard Out
Questions
Use the following transcript to answer the next 3 questions.
4. Which of the following command lines would append lines to the (already existing) file df.out?
( ) a. df > df.out
( ) b. df >+ df.out
( ) c. df.out << df
( ) d. df.out +< df
( ) e. None of the above
rha030-5.0-2-en-2007-10-16T13:03:19-0400 17
Copyright (c) 2003-2007 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use is a violation of U.S.
and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whether in electronic or print format without
prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributed please email training@redhat.com or phone toll-
free (USA) +1 866 626 2994 or +1 (919) 754 3700.
Chapter 1. Standard In and Standard Out
[prince@station prince]$ ps
PID TTY TIME CMD
1409 pts/0 00:00:00 bash
1542 pts/0 00:00:00 vmstat
1543 pts/0 00:00:00 ps
[prince@station prince]$ ls -l /proc/1542/fd
total 6
lrwx------ 1 prince prince 64 Sep 22 20:42 0 -> /dev/pts/0
l-wx------ 1 prince prince 64 Sep 22 20:42 1 -> /tmp/vmstat.out
lrwx------ 1 prince prince 64 Sep 22 20:42 2 -> /dev/pts/0
lr-x------ 1 prince prince 64 Sep 22 20:42 3 -> /proc/uptime
lr-x------ 1 prince prince 64 Sep 22 20:42 4 -> /proc/stat
lr-x------ 1 prince prince 64 Sep 22 20:42 5 -> /proc/meminfo
5. Which of the following files is the vmstat command using for Standard Out?
( ) a. /proc/stat
( ) b. /dev/tty1
( ) c. /dev/pts/0
( ) d. /tmp/vmstat.out
( ) e. There is not enough information provided.
6. Which of the following occurred when the vmstat command was started?
( ) a. The bash shell created a new file.
( ) b. The bash shell clobbered an already existing file.
( ) c. The bash shell appended to an already existing file.
( ) d. The vmstat command created a new file.
( ) e. There is not enough information provided.
7. Which of the following files is the vmstat command using for Standard In?
( ) a. /proc/stat
( ) b. /tmp/vmstat.out
( ) c. /dev/pts/0
( ) d. /dev/tty1
( ) e. There is not enough information provided.
rha030-5.0-2-en-2007-10-16T13:03:19-0400 18
Copyright (c) 2003-2007 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use is a violation of U.S.
and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whether in electronic or print format without
prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributed please email training@redhat.com or phone toll-
free (USA) +1 866 626 2994 or +1 (919) 754 3700.
Chapter 1. Standard In and Standard Out
9. For the cat process, to what file would the symbolic link /proc/self/fd/0 resolve?
( ) a. /dev/stdin
( ) b. cal.out
( ) c. /dev/tty1
( ) d. /tmp
( ) e. /proc/self
10. For the cat process, to what file would the symbolic link /proc/self/fd/1 most likely resolve?
( ) a. /proc/self
( ) b. /tmp
( ) c. cal.out
( ) d. /dev/stdin
( ) e. /dev/tty1
Notes
1. This is an oversimplification. Some commands do respond differently if stdin or stdout is a terminal
instead of a file. The mail command, for instance, prompted elvis with a Cc: prompt when stdin was
a terminal, but not when stdin was the file sluggish.txt. In general, however, commands should
be thought of reading from a file or a terminal interchangeably, with only occasional, minor changes
in behavior.
rha030-5.0-2-en-2007-10-16T13:03:19-0400 19
Copyright (c) 2003-2007 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other
use is a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise
duplicated whether in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or
otherwise improperly distributed please email training@redhat.com or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.
Chapter 2. Standard Error
Key Concepts
• Unix programs commonly report error conditions to a destination called Standard Error (stderr).
• Usually, stderr is connected to a terminal’s display, and error messages are found intermixed with
standard output.
• When using the bash shell, the stderr stream can be redirected to a file using 2>.
• When using bash, the stderr stream can be combined with stdout stream using 2>&1 or >&
Discussion
20
Chapter 2. Standard Error
The head command, when fed multiple file names as arguments, conveniently decorates the name of the
file, followed by the first specified number of lines (in this case, one). When the head command
encounters a directory, however, it merely complains. Next, elvis runs the same command, redirecting
stdout to the file rcsummary.out.
Most of the output is obediently redirected to the file rcsummary.out, but the directory complaints are
still displayed. Although not obvious at the outset, the head command is really sending output to two
independent streams. Normal output is written to Standard Out, but error message are written to a
separate stream called Standard Error (often abbreviated stderr). Usually, both streams are connected to
the terminal, and so the two are difficult to distinguish. By redirecting stdout, however, the information
written to stderr is obvious.
Redirecting stderr
Just as bash uses > to redirect stdout, bash uses 2> to redirect stderr. For example, elvis repeats the head
command from above, but instead of redirecting stdout to rcsummary.out, he redirects stderr to the file
rcsummary.err.
rha030-5.0-2-en-2007-10-16T13:03:19-0400 21
Copyright (c) 2003-2007 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use is
a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whether
in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributed
please email training@redhat.com or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.
Chapter 2. Standard Error
The output is the complement to the previous example. We now see the normal output displayed to the
screen, but no error messages. Where did the error messages go? It shouldn’t be hard to guess.
In the following example, both > and 2> are used to redirect stdout and stderr independently.
In this case, the standard output can be found in the file rcsummary.out, error messages can be found
in rcsummary.err, and nothing is left over to be displayed to the screen.
rha030-5.0-2-en-2007-10-16T13:03:19-0400 22
Copyright (c) 2003-2007 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use is
a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whether
in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributed
please email training@redhat.com or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.
Chapter 2. Standard Error
Upon examining the file rcsummary.both, however, elvis doesn’t find what he expects.
The bash shell opened the file rcsummary.both twice, but treated each open file independently. When
stdout and stderr both wrote to the file, they clobbered each other’s information. What is needed instead
is some way to tell bash to effectively combine stderr and stdout into a single stream, and then redirect
that stream to a single file. As you would expect, there is such a way.
Although awkward, the last token 2>&1 should be thought of as saying "take stderr, and send it
wherever stdout is currently going". Now rcsummary.both contains the expected output.
rha030-5.0-2-en-2007-10-16T13:03:19-0400 23
Copyright (c) 2003-2007 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other
use is a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise
duplicated whether in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or
otherwise improperly distributed please email training@redhat.com or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.
Chapter 2. Standard Error
Summary
The following table summarizes the syntax used by the bash shell for redirecting stdin, stdout, and stderr
learned in this and the previous lesson.
syntax effect
cmd < file Redirect stdin from file
cmd > file Redirect stdout into file, overwriting (clobbering) file if it exists.
cmd >> file Redirect stdout into file, appending to file if it exists.
cmd 2> file Redirect stderr into file, overwriting (clobbering) file if it exists.
cmd 2>> file Redirect stderr into file, appending to file if it exists.
cmd > file 2>&1 Combine stdout and stderr, and redirect both into file. (Portable syntax)
cmd >& file Combine stdout and stderr, and redirect both into file. (Convenient syntax)
Examples
rha030-5.0-2-en-2007-10-16T13:03:19-0400 24
Copyright (c) 2003-2007 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use is
a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whether
in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributed
please email training@redhat.com or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.
Chapter 2. Standard Error
(Much of the output of this command has been truncated, and replaced with "...").
Although the find command appropriately reported the /var/spool/mail/elvis file, the output is
difficult to find among all of the "Permission denied" error messages being reported from various
subdirectories of /var. In order to help separate the wheat from the chaff, elvis redirects stderr to some
file in the /tmp directory.
While this works, elvis is left with a file called /tmp/foo that he really didn’t want. In situations like
this, when a user wants to discard a stream of information, experienced Unix users usually redirect
output to a pseudo device called /dev/null.
As the following long listing shows, /dev/null is a character device node, like those used for
conventional device drivers.
When a user writes to /dev/null, the information is merely discarded by the kernel. When a user reads
from /dev/null, they encounter an immediate end of file. Notice that /dev/null is one of the few
files in Red Hat Enterprise Linux that has world writable permissions by default.
Online Exercises
Lab Exercise
Objective: Effectively manage Standard Out and Standard Error Streams
Estimated Time: 10 mins.
Specification
1. Use the following command line to display the contents of all files within the /etc/X11 directory.
[elvis@station elvis]$ cat /etc/X11/*
cat: /etc/X11/applnk: Is a directory
rha030-5.0-2-en-2007-10-16T13:03:19-0400 25
Copyright (c) 2003-2007 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other
use is a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise dupli-
cated whether in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or other-
wise improperly distributed please email training@redhat.com or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.
Chapter 2. Standard Error
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin
...
2. Repeat the command line, but redirect stdout to a file called ~/stderrlab.out and stderr to a file
called ~/stderrlab.err.
3. Repeat the command again, but combine stdout and stderr into a single stream, and redirect the
stream to the file ~/stderrlab.both.
Deliverables
1. A file called ~/stderrlab.out, which contains the stdout stream from the command cat /etc/X11/*.
2. A file called ~/stderrlab.err, which contains the stderr stream from the command cat /etc/X11/*.
3. A file called ~/stderrlab.both, which contains the combined stdout and stderr streams from the command
cat /etc/X11/*.
Questions
Use the following transcript to answer the next question.
1. Which of the following command lines most likely created the file chmod.err?
( ) a. chmod a+r /tmp/* > chmod.err
( ) b. chmod a+r /tmp/* 2> chmod.err
( ) c. chmod a+r /tmp/* >> chmod.err
( ) d. chmod a+r /tmp/* e> chmod.err
( ) e. None of the above
rha030-5.0-2-en-2007-10-16T13:03:19-0400 26
Copyright (c) 2003-2007 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use is a violation
of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whether in electronic or print
format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributed please email
training@redhat.com or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.
Chapter 2. Standard Error
2. Which of the following command lines would combine stdout and stderr, and redirect the combined stream to the
file /tmp/find.out?
( ) a. find /etc > /tmp/find.out 2>&1
( ) b. find /etc >> /tmp/find.out
( ) c. find /etc >& /tmp/find.out
( ) d. find /etc >>& /tmp/find.out
( ) e. Both A and C
3. Which of the following command lines would combine stdout and stderr, and redirect the combined stream to the
file /tmp/find.out, appending to the file if it already existed?
( ) a. find /etc > /tmp/find.out 2>&1
( ) b. find /etc >> /tmp/find.out
( ) c. find /etc >> /tmp/find.out 2>&1
( ) d. find /etc >>& /tmp/find.out
( ) e. Both A and C
rha030-5.0-2-en-2007-10-16T13:03:19-0400 27
Copyright (c) 2003-2007 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use is a violation
of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whether in electronic or print
format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributed please email
training@redhat.com or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.
Chapter 2. Standard Error
( ) d. 3
( ) e. None of the above
5. Which of the following command lines could replace the question marks as the last command line in the
transcript?
( ) a. cat /dev/pts/1
( ) b. cat /tmp/cat.out
( ) c. cat /tmp/cat.err
( ) d. cat /etc/termcap
( ) e. cat /dev/null
6. To which of the following files would you expect the symbolic link /dev/stderr to resolve?
( ) a. ../proc/self/fd/2
( ) b. ../proc/self/fd/1
( ) c. ../proc/self/fd/0
( ) d. /dev/null
( ) e. None of the above
if id $1
then
echo "The user $1 belongs to the following groups: $(id -Gn $1) "
else
echo "The user $1 does not exist"
fi
[madonna@station madonna]$ ./groups.sh elvis
uid=501(elvis) gid=501(elvis) groups=501(elvis),201(wrestle),202(physics),203(emperors),205(music)
The user elvis belongs to the following groups: elvis wrestle physics emperors music
[madonna@station madonna]$ ./groups.sh barney
id: barney: No such user
The user barney does not exist
rha030-5.0-2-en-2007-10-16T13:03:19-0400 28
Copyright (c) 2003-2007 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use is a violation
of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whether in electronic or print
format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributed please email
training@redhat.com or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.
Chapter 2. Standard Error
7. Which of the following replacements for line number 3 of the file groups.sh would cause the script to display a
single line beginning "The user", in all cases?
( ) a. if id -q $1
( ) b. if id $1 >/dev/null 2>&1
( ) c. if id $1 2> /dev/null
( ) d. if id $1 > /dev/null
( ) e. None of the above
8. Which of the following replacements for line number 7 of the file groups.sh would cause the script to complain
on Standard Error if the user does not exist (instead of Standard Out)?
( ) a. echo "The user $1 does not exist" > /dev/stderr
( ) b. echo -e "The user $1 does not exist"
( ) c. echo "The user $1 does not exist" > /proc/self/fd/2
( ) d. echo -E "The user $1 does not exist"
( ) e. A and C
9. Assuming cmd is some simple command and its arguments (but no shell metacharacters), which of the following
would redirect Standard Error (only) to the file /tmp/errors, appending to the file if it already existed?
( ) a. cmd 2> /tmp/errors
( ) b. cmd >> /tmp/errors
( ) c. cmd >+ /tmp/errors
( ) d. cmd 2>+ /tmp/errors
( ) e. cmd 2>> /tmp/errors
10. Assuming cmd is some simple command and its arguments (but no shell metacharacters), which of the following
would be guaranteed to execute cmd , but generate no visible output?
( ) a. cmd 2> /dev/null
( ) b. cmd > /dev/null
( ) c. (cmd )
( ) d. cmd > /dev/null 2>&1
( ) e. silent (cmd )
rha030-5.0-2-en-2007-10-16T13:03:19-0400 29
Copyright (c) 2003-2007 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other
use is a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise
duplicated whether in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or
otherwise improperly distributed please email training@redhat.com or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.
Chapter 3. Pipes
Key Concepts
• The stdout stream from one process can be connected to the stdin stream of another process, using
what Unix calls a "pipe".
• Many commands in Unix are designed to operate as a filter, reading input from stdin and sending
output to stdout.
• bash uses "|" to create a pipe between two commands.
Discussion
Pipes
In the previous Lessons, we have seen that a process’s output can be redirected to somewhere other than
the terminal display, or that a process can be asked to read input from some location other than the
terminal keyboard. One of the most common, and most powerful, forms of redirection is a combination
of the two, where the output (Standard Out) of one command is "piped" directly into the input (Standard
In) of another command, forming what Linux (and Unix) refers to as a pipe.
When two commands are joined by a pipe, the stdout stream of the first process is tied directly to the
stdin sequence of the second process, so that multiple processes can be combined in a sequence. In order
to create a pipe using bash, the two commands are joined with a vertical bar |. (On most keyboards, this
character is found on the same key as the backslash, above the RETURN key.) All processes that are
joined in a pipe are referred to as a process group.
As an example, consider prince, who is trying to find the largest files underneath the /etc directory. He
begins by composing a find command that will list all file with a size greater than 100Kbytes.
Observing that the find command seems to list the files in no particular order, prince decides he would
like the files to be listed alphabetically. He could redirect the output to a file, and then sort the file.
Instead, he takes advantage of the fact that the sort command, when invoked without arguments, looks to
Standard In for the data to sort. He pipes the output of his find command into sort.
30
Chapter 3. Pipes
...
/etc/makedev.d/cciss
/etc/makedev.d/dac960
/etc/squid/squid.conf
/etc/squid/squid.conf.default
/etc/termcap
The first argument to the grep command is the string of text to be searched for, and any remaining
arguments are files to be searched for the text. If the grep command is called with only one argument (a
string to be searched for, but no files to search), it looks to Standard In as its source of data on which to
operate.
In the following, prince has so many files in his home directory that he is having trouble keeping track of
them. He’s trying to find a directory called templates that he created a few months ago. He uses the
locate command to help him find it.
Unfortunately for prince, there are many files which contain the text templates in their name on the
system, and prince becomes overwhelmed with lines and lines of output. In order to reduce the
information to more relevant files, prince next takes stdout from the locate command, and creates a pipe
to stdin of the grep command, "grepping" for the word "prince".
rha030-5.0-2-en-2007-10-16T13:03:19-0400 31
Copyright (c) 2003-2007 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other
use is a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise
duplicated whether in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or
otherwise improperly distributed please email training@redhat.com or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.
Chapter 3. Pipes
Because the grep command is not given a file to search, it looks to stdin, where it finds the stdout stream
of the locate command. Filtering the stream, grep only duplicates to its stdout lines that matched the
specified text, "prince". The rest were discarded. The user prince easily finds his directory under
~/proj, as well as another directory created by the application quanta.
Again, prince is overwhelmed by the amount of output from this command. He tries the same trick,
"grepping" it down for all lines that contain the word "passwd".
While stdout from the first grep command was appropriately filtered, stderr is unaffected, and still gets
displayed to the screen. How would prince go about suppressing stderr as well?
rha030-5.0-2-en-2007-10-16T13:03:19-0400 32
Copyright (c) 2003-2007 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other
use is a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise
duplicated whether in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or
otherwise improperly distributed please email training@redhat.com or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.
Chapter 3. Pipes
Commands as filters
The concept of a pipe extends naturally, so that multiple commands can be used together, each reading
information from stdin, somehow modifying or filtering the information, and passing the result to stdout.
In a subsequent Workbook, you will find that there are many standard Linux (and Unix) commands that
are designed for this purpose, including some that you are already familiar with: grep, head, tail, cut,
sort, sed, and awk, to name a few.
Examples
rha030-5.0-2-en-2007-10-16T13:03:19-0400 33
Copyright (c) 2003-2007 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use is
a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whether
in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributed
please email training@redhat.com or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.
Chapter 3. Pipes
He now locates the command line he wanted, and uses history substitution to easily repeat the command.
...
May 27 12:18:21 server1 dhcpd: DHCPACK on 192.168.0.110 to 00:09:6b:d0:ce:8f via eth0
May 27 12:18:27 server1 login(pam_unix)[1981]: session closed for user root
May 27 12:19:15 server1 named[24350]: listening on IPv4 interface eth1, 192.168.22.20#53
May 27 12:19:21 server1 vsftpd: warning: can’t get client address: Bad file descriptor
May 27 12:19:21 server1 last message repeated 3 times
May 27 12:20:27 server1 dhcpd: DHCPDISCOVER from 00:08:74:37:c5:c3 via eth0
May 27 12:20:27 server1 dhcpd: DHCPOFFER on 192.168.0.11 to 00:08:74:37:c5:c3 via eth0
May 27 12:20:27 server1 dhcpd: DHCPREQUEST for 192.168.0.11 (192.168.0.254) from 00:08:74:37
...
Without worrying about the details of the DHCP protocol, suppose the administrator wanted to extract a
list of IP addresses and the hardware addresses they were offered to from the log file. An experienced
administrator might take the following approach.
Realizing that the /var/log/message file is a very large file, in this case over 1000 lines in length, the
administrator first uses the grep command to reduce the information to only the relevant lines.
rha030-5.0-2-en-2007-10-16T13:03:19-0400 34
Copyright (c) 2003-2007 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use is
a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whether
in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributed
please email training@redhat.com or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.
Chapter 3. Pipes
This is a start, but the administrator is still dealing with too much information (the above screenshot only
lists the first 5 of 90 lines produced by this command). In order to extract just the relevant information,
namely the IP address and hardware address, the administrator takes the output of the grep command,
and pipes it to a command called sed, which strips the first few words from each line.
If you are not familiar with the sed command (and you probably aren’t), don’t worry about the details.
Just note that sed’s argument had the effect of removing the leading text from each line, up to the word
"on". There’s still too much extra text, however, so the administrator takes the output of this grep-sed
combination, and pipes it to a command called awk.
Again, not worrying about the details of the awk command, note that the result was to extract just the
first and third column from the previous output. In order to sort the data, and remove duplicate lines, the
administrator now takes the output from the chain, and pipes it in turn through the commands sort and
uniq.
rha030-5.0-2-en-2007-10-16T13:03:19-0400 35
Copyright (c) 2003-2007 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use is
a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whether
in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributed
please email training@redhat.com or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.
Chapter 3. Pipes
192.168.0.15 00:08:74:37:c8:eb
192.168.0.16 00:08:74:d9:41:ac
192.168.0.17 00:08:74:35:00:e3
192.168.0.2 00:08:74:d9:41:32
192.168.0.3 00:08:74:d9:40:a4
192.168.0.4 00:08:74:d9:3f:7f
192.168.0.5 00:08:74:34:fd:36
192.168.0.6 00:08:74:d9:41:a3
192.168.0.7 00:08:74:d9:41:53
192.168.0.8 00:08:74:d9:41:7b
192.168.0.9 00:08:74:35:00:1f
This is almost the list that the administrator wants, but the sort command didn’t work quite right. The
information is sorted, but it’s sorted alphabetically, not by IP address. The administrator modifies the
sort command with a couple of command line switches, specifying to sort numerically, keying on the
fourth field, where fields are separated by a period:
[root@station log]$ grep DHCPOFFER messages | sed "s/^.*on //" | awk ’{print $1,
$3}’ | sort -n -k4 -t. | uniq
192.168.0.1 00:08:74:d9:41:9e
192.168.0.2 00:08:74:d9:41:32
192.168.0.3 00:08:74:d9:40:a4
192.168.0.4 00:08:74:d9:3f:7f
192.168.0.5 00:08:74:34:fd:36
192.168.0.6 00:08:74:d9:41:a3
192.168.0.7 00:08:74:d9:41:53
192.168.0.8 00:08:74:d9:41:7b
192.168.0.9 00:08:74:35:00:1f
192.168.0.10 00:08:74:d9:40:95
192.168.0.11 00:08:74:37:c5:c3
192.168.0.12 00:08:74:d9:41:dd
192.168.0.13 00:08:74:35:00:d0
192.168.0.14 00:08:74:34:fe:bc
192.168.0.15 00:08:74:37:c8:eb
192.168.0.16 00:08:74:d9:41:ac
192.168.0.17 00:08:74:35:00:e3
192.168.0.110 00:09:6b:d0:ce:8f
This is the list that the administrator wanted, in the order that she wanted. She redirects all of this output
to a file in her home directory.
[root@station log]$ grep DHCPOFFER messages | sed "s/^.*on //" | awk ’{print $1
,$3}’ | sort -n -k4 -t. | uniq > ~/ip_dhcp.txt
In this example, an (admittedly experienced) administrator browsing a log file was able to spend a few
minutes and develop a chain of commands that filtered the original information down to exactly the
information that she wanted. She did this with a handful of tools that are in most Unix administrator’s
mental toolkit: grep, sed, awk, sort, and uniq.
If the administrator were using an operating system that wasn’t designed around the philosophy "small
tools that work together", she would have needed to rely on some programmer to develop the
ip_mac_extractor utility, and possibly rely on that programmer to create a graphical user interface for
rha030-5.0-2-en-2007-10-16T13:03:19-0400 36
Copyright (c) 2003-2007 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other
use is a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise
duplicated whether in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or
otherwise improperly distributed please email training@redhat.com or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.
Chapter 3. Pipes
the utility as well. Instead, because she was able to use the flexibility of the command line, she was able
to handle the information herself.
Online Exercises
Lab Exercise
Objective: Use pipes to effectively filter information.
Estimated Time: 10 mins.
Specification
1. You would like to create a sorted list of all TCP services found in the file /etc/services. Pipe the
output of the command grep tcp /etc/services into the command sort. Redirect the output of this
pipe into the file ~/pipelab.txt.
2. Using the less pager, you would like to browse the output of the ls -R / command, viewing only files
that contain the letter s. Compose a command line using two pipes to chain the commands ls -R /,
grep s, and less. Leave the less pager in the foreground while you grade your exercise.
Deliverables
1. A file called ~/pipelab.txt, which contains the output of the command grep tcp /etc/services piped
through the command sort.
2. An active less pager, which is browsing the output of the command ls -R /, piped through the command grep s.
Clean Up
After your Exercise has been graded, you may quit the less pager.
Questions
1. Which of the following command lines would reduce the output of the locate conf command to only files whose
rha030-5.0-2-en-2007-10-16T13:03:19-0400 37
Copyright (c) 2003-2007 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use is a violation
of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whether in electronic or print
format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributed please email
training@redhat.com or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.
Chapter 3. Pipes
2. Which of the following command lines would produce a sorted list of all processes which contain the text sshd?
( ) a. ps aux | sort | grep sshd
( ) b. ps aux | grep sshd | sort
( ) c. grep sshd | ps aux | sort
( ) d. sort | ps aux | grep sshd
( ) e. A or B
rha030-5.0-2-en-2007-10-16T13:03:19-0400 38
Copyright (c) 2003-2007 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use is a violation
of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whether in electronic or print
format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributed please email
training@redhat.com or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.
Chapter 3. Pipes
8. When bash reports the stopped process group, the ls alias has been expanded to ls --color=tty. The --color=tty
option tells the ls command to generate color control sequences only if its stdout is tied to a terminal. Which of the
following is true?
( ) a. The ls command generates color control sequences, which can be observed by the less pager.
( ) b. The ls command does not generate color control sequences.
( ) c. The ls command generates color control sequences, but they are filtered out by the grep command.
rha030-5.0-2-en-2007-10-16T13:03:19-0400 39
Copyright (c) 2003-2007 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other use is a violation
of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise duplicated whether in electronic or print
format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or otherwise improperly distributed please email
training@redhat.com or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.
Chapter 3. Pipes
( ) d. The ls command never generates color control sequences (the --color=tty is only included for purposes of
backwards compatibility).
( ) e. None of the above apply.
9. Which of the following command lines would allow the less pager to browse grep’s error messages, as well as
output?
( ) a. grep root /etc/* 2>&1 | less
( ) b. grep root /etc/* | less 2>&1
( ) c. grep root /etc/* >>| less
( ) d. grep root /etc/* 2| less
( ) e. None of the above
The -j command line switch causes the ps command to generate "job control oriented" output. Use the following
transcript to answer the next question.
10. Which of the following would be the most reasonable expansion of the second column header, PGID?
( ) a. Process Group ID
( ) b. Parent Generation ID
( ) c. Process Gender ID
( ) d. Powder Gondola ID
( ) e. None of the above
rha030-5.0-2-en-2007-10-16T13:03:19-0400 40
Copyright (c) 2003-2007 Red Hat, Inc. All rights reserved. For use only by a student enrolled in a Red Hat Academy course taught at a Red Hat Academy. Any other
use is a violation of U.S. and international copyrights. No part of this publication may be photocopied, duplicated, stored in a retrieval system, or otherwise
duplicated whether in electronic or print format without prior written consent of Red Hat, Inc. If you believe Red Hat course materials are being used, copied, or
otherwise improperly distributed please email training@redhat.com or phone toll-free (USA) +1 866 626 2994 or +1 (919) 754 3700.