Published 2005-08-17
Table of Contents
1. Standard In and Standard Out ............................................................................................................ 4
Discussion .......................................................................................................................................... 4
Three types of programs........................................................................................................... 4
Standard in (stdin) and Standard out (stdout)........................................................................... 5
Redirecting stdout..................................................................................................................... 5
Redirecting stdin....................................................................................................................... 6
Under the Hood: Open Files and File Descriptors ................................................................... 7
Examples............................................................................................................................................ 9
Example 1. Getting Out of sorts ............................................................................................. 9
Example 2. Automated FTP Transfers ................................................................................... 10
Example 3. Automating Graph Generation with gnuplot......................................................13
Online Exercises............................................................................................................................... 15
Online Exercise 1. Using Standard In and Standard Out ....................................................... 15
Questions.......................................................................................................................................... 16
2. Standard Error .................................................................................................................................... 20
Discussion ........................................................................................................................................ 20
Standard Error (stderr)............................................................................................................ 20
Redirecting stderr ................................................................................................................... 21
Combining stdout and stderr: Old School .............................................................................. 22
Combining stdout and stderr: New School............................................................................. 23
Summary................................................................................................................................. 24
Examples.......................................................................................................................................... 24
Example 1. Using /dev/null to filter out stderr .................................................................. 24
Online Exercises............................................................................................................................... 25
Specification ........................................................................................................................... 25
Deliverables ............................................................................................................................ 26
Questions.......................................................................................................................................... 26
3. Pipes ...................................................................................................................................................... 30
Discussion ........................................................................................................................................ 30
Pipes ....................................................................................................................................... 30
Filtering output using grep..................................................................................................... 31
Pipes and stderr....................................................................................................................... 32
Commands as filters ............................................................................................................... 32
Examples.......................................................................................................................................... 33
Example 1. Listing Processes by Name ................................................................................. 33
Example 2. Searching Command History Efficiently ............................................................ 33
Example 3. Unix philosophy: Simple Tools that Work Well Together...................................34
Online Exercises............................................................................................................................... 37
Specification ........................................................................................................................... 37
Deliverables ............................................................................................................................ 37
Clean Up ................................................................................................................................. 37
Questions.......................................................................................................................................... 37
iii
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
terminals keyboard.
The destination programs write to is referred to as Standard Out (stdout), and is usually connected to a
terminals 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, youll 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.
Redirecting stdout
Writing Output to a File
When a terminal based program generates output, it generally writes that output to its stdout stream,
without knowing what is connected to the receiving end of that stream. Usually, the stdout stream is
connected to the terminal that started the process, so the output is written to the terminals display. The
bash shell uses > to redirect a processs stdout stream to a file.
For example, suppose the machine elvis is using becomes very sluggish and non-responsive. In order to
diagnose the problem, elvis would like to examine the currently running processes. Because the machine
is so sluggish, however, he wants to collect the information now, but analyze it later. He can redirect the
output of the ps aux command into the file sluggish.txt, and come back to examine the file when the
machine is more responsive.
[elvis@station elvis]$ ps aux > sluggish.txt
[elvis@station elvis]$
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.
[elvis@station elvis]$ head sluggish.txt
USER
root
root
root
root
root
root
root
root
root
rha030-3.0-0-en-2005-08-17T07:23:17-0400
VSZ
1380
0
0
0
0
0
0
0
0
RSS
76
0
0
0
0
0
0
0
0
TTY
?
?
?
?
?
?
?
?
?
STAT
S
SW
SW
SWN
SW
SW
SW
SW
SW
START
Jun02
Jun02
Jun02
Jun02
Jun02
Jun02
Jun02
Jun02
Jun02
TIME
0:04
0:00
0:00
0:00
0:00
0:00
0:00
0:37
0:00
COMMAND
init [
[keventd]
[kapmd]
[ksoftirqd_CPU0]
[bdflush]
[kswapd]
[kscand/DMA]
[kscand/Normal]
[kscand/HighMem]
Copyright (c) 2003-2005 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.
Tue Jun
USER
root
root
root
root
root
root
root
root
RSS
76
0
0
0
0
0
0
0
TTY
?
?
?
?
?
?
?
?
STAT
S
SW
SW
SWN
SW
SW
SW
SW
START
Jun02
Jun02
Jun02
Jun02
Jun02
Jun02
Jun02
Jun02
TIME
0:04
0:00
0:00
0:00
0:00
0:00
0:00
0:37
COMMAND
init [
[keventd]
[kapmd]
[ksoftirqd_CPU0]
[bdflush]
[kswapd]
[kscand/DMA]
[kscand/Normal]
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.
[elvis@station elvis]$ mail
Hey sysadmin...
Im 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 commands stdin stream to be read from the file.
[elvis@station elvis]$ mail -s "ps output" sysadmin@example.com < sluggish.txt
rha030-3.0-0-en-2005-08-17T07:23:17-0400
Copyright (c) 2003-2005 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.
[1]+
Stopped
cat /etc/termcap
Using the ps command to look up the processs PID, elvis next examines the processs /proc/pid /fd
directory.
[elvis@station elvis]$ ps
PID
1368
1910
1911
TTY
pts/1
pts/1
pts/1
TIME
00:00:00
00:00:00
00:00:00
CMD
bash
cat
ps
total 0
lrwx-----lrwx-----lrwx-----lr-x------
1
1
1
1
elvis
elvis
elvis
elvis
elvis
elvis
elvis
elvis
64
64
64
64
Sep
Sep
Sep
Sep
13
13
13
13
06:42
06:42
06:42
06:42
0
1
2
3
->
->
->
->
elvis now looks in the subdirectory which corresponds to the observed PID.
rha030-3.0-0-en-2005-08-17T07:23:17-0400
/dev/tty1
/dev/tty1
/dev/tty1
/etc/termcap
Copyright (c) 2003-2005 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.
Descriptor
Abbreviation
Standard In
stdin
Standard Out
stdout
Standard Error
stderr
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 elviss
terminal. What happens if the cat process reads from stdin? It reads input from elviss keyboard. What
happens if it writes to stdout? Whatever is written is displayed on elviss terminal.
Redirection
In the next example, elvis cats 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.
[elvis@station elvis]$ cat /etc/termcap > /tmp/foo
[1]+
Stopped
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
1368
1976
1977
TTY
pts/1
pts/1
pts/1
TIME
00:00:00
00:00:00
00:00:00
CMD
bash
cat
ps
total 0
lrwx-----l-wx-----lrwx-----lr-x------
1
1
1
1
elvis
elvis
elvis
elvis
elvis
elvis
elvis
elvis
64
64
64
64
Sep
Sep
Sep
Sep
13
13
13
13
07:05
07:05
07:05
07:05
0
1
2
3
->
->
->
->
/dev/pts/1
/tmp/foo
/dev/pts/1
/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-3.0-0-en-2005-08-17T07:23:17-0400
Copyright (c) 2003-2005 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.
[1]+
Stopped
[elvis@station elvis]$ ps
PID
1368
1980
1988
TTY
pts/1
pts/1
pts/1
TIME
00:00:00
00:00:00
00:00:00
CMD
bash
cat
ps
total 0
lr-x-----l-wx-----lrwx------
1 elvis
1 elvis
1 elvis
elvis
elvis
elvis
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?
[elvis@station elvis]$ cat < /etc/termcap > /tmp/foo
[elvis@station elvis]$ cat /etc/termcap > /tmp/foo
[elvis@station elvis]$ cp /etc/termcap /tmp/foo
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.
elephant
seal
rha030-3.0-0-en-2005-08-17T07:23:17-0400
Copyright (c) 2003-2005 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.
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.
[blondie@station blondie]$ sort < zoo
ape
elephant
fish
giraffe
seal
While this behavior seems (and is) perfectly reasonable, it often confuses new users who innocently type
a commands 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 shes started.
Finally, a friend whispers to her, "CTRL-D".
[blondie@station blondie]$ sort
ls
quit
man sort
exit
get me out of this
CTRL-D
exit
get me out of this
ls
man sort
quit
[blondie@station blondie]$
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-3.0-0-en-2005-08-17T07:23:17-0400
10
Copyright (c) 2003-2005 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.
331 Anonymous login ok, send your complete email address as your password.
Password: (blondie types in her email address)
227 Entering
g150 Opening
drwxrws---r--r--r--r--r--r-drwxrwsr-x
...
226 Transfer
RCS
README
README_ABOUT_BZ2_FILES
dist
complete.
221 Goodbye.
When the ftp command pauses with the ftp> prompt, blondie types commands to navigate the ftp
servers 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).
[blondie@station blondie]$ cat getreadme.ftp
anonymous
blondie@example.com
ls
cd pub
ls
rha030-3.0-0-en-2005-08-17T07:23:17-0400
11
Copyright (c) 2003-2005 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.
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.
[blondie@station blondie]$ ftp ftp.kernel.org < getreadme.ftp
Password:Name (ftp.kernel.org:blondie):
?Invalid command
drwxr-s--2 korg
mirrors
4096
drwx-----2 root
root
16384
drwxrwsr-x
8 korg
korg
4096
drwxrws--2 korg
korg
4096
-r--r--r-1 korg
korg
1963
-r--r--r-1 korg
korg
578
drwxrwsr-x
4 korg
korg
4096
-r--r--r-1 korg
korg
1507
drwxrwsr-x
8 korg
korg
4096
drwxrwsr-x
3 korg
korg
4096
drwxrwsr-x
3 korg
korg
4096
drwxrwsr-x 11 korg
korg
4096
May
Mar
Mar
Mar
Oct
Mar
Oct
Oct
Jan
Mar
Oct
Jan
21
18
24
18
4
18
26
11
21
24
11
1
2001
00:27
17:46
04:05
2001
04:04
2000
2001
2002
17:46
2001
2002
for_mirrors_only
lost+found
pub
RCS
README
README_ABOUT_BZ2_FILES
dist
index.html
linux
scm
site
software
-rw-rw-r--
1 blondie
blondie
1963 Jun
3 17:37 README
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 didnt 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 users home directory, and if it exists, use it to provide the users username and
password. After examining the netrc(5) man page, blondie uses a simple text editor to create the
following ~/.netrc file.
[blondie@station blondie]$ cat .netrc
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-3.0-0-en-2005-08-17T07:23:17-0400
12
Copyright (c) 2003-2005 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.
cd pub
get README
quit
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.
[blondie@station blondie]$ head .netrc getreadme.ftp
getreadme.ftp
[blondie@station blondie]$ ftp ftp.kernel.org < getreadme.ftp
[blondie@station blondie]$ ls
getreadme.ftp
README
procs
r b
2 6
1 5
1 6
0 6
0 6
3 3
3 3
1 6
swpd
0
0
0
0
0
0
0
0
free
17348
15736
11496
10492
10168
8848
8056
12248
buff
65604
66008
67224
67944
68324
69424
70188
71084
memory
cache
277768
277788
277392
277676
277644
277864
277892
277636
si
0
0
0
0
0
0
0
0
swap
so
0
0
0
0
0
0
0
0
bi
15
376
1216
940
576
1252
1068
940
io
bo
16
6269
8
28
0
64
1148
28
system
cpu
in
cs us sy id wa
126
221 1 0 97 1
314
725 5 2 0 93
422 1533 15 16 0 69
338 1193 7 8 0 85
261
992 6 1 0 93
429 1386 10 16 0 74
422 1215 8 16 0 76
341 1275 9 4 0 87
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-3.0-0-en-2005-08-17T07:23:17-0400
13
Copyright (c) 2003-2005 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.
G N U P L O T
Version 3.7 patchlevel 3
...
Terminal type set to x11
gnuplot> set term png
After quiting gnuplot, she returns to the bash shell, where she uses the eog image viewer to view her
plot.
[madonna@station madonna]$ eog cpu.png
rha030-3.0-0-en-2005-08-17T07:23:17-0400
14
Copyright (c) 2003-2005 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.
Now she can easily plot newly collected data by redirecting her script as gnuplots stdin.
[madonna@station madonna]$ gnuplot < cpu_plot.gnuplot
Online Exercises
Online Exercise 1. Using Standard In and Standard Out
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 stations 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
15
rha030-3.0-0-en-2005-08-17T07:23:17-0400
Copyright (c) 2003-2005 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.
script.gnuplot
[student@station student]$ gnuplot < script.gnuplot
[student@station student]$ ls
gnuplot.png
script.gnuplot
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 ...
gnuplot> set term png
gnuplot> set output "gnuplot.png"
... 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-3.0-0-en-2005-08-17T07:23:17-0400
16
Copyright (c) 2003-2005 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 tollfree (USA) +1 866 626 2994 or +1 (919) 754 3700.
Questions
Use the following transcript to answer the next 3 questions.
[prince@station prince]$ who
prince
tty1
Sep 22 20:02
rha030-3.0-0-en-2005-08-17T07:23:17-0400
17
Copyright (c) 2003-2005 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 tollfree (USA) +1 866 626 2994 or +1 (919) 754 3700.
PID
1409
1542
1543
TTY
pts/0
pts/0
pts/0
TIME
00:00:00
00:00:00
00:00:00
CMD
bash
vmstat
ps
total 6
lrwx-----l-wx-----lrwx-----lr-x-----lr-x-----lr-x------
1
1
1
1
1
1
prince
prince
prince
prince
prince
prince
prince
prince
prince
prince
prince
prince
64
64
64
64
64
64
Sep
Sep
Sep
Sep
Sep
Sep
22
22
22
22
22
22
20:42
20:42
20:42
20:42
20:42
20:42
0
1
2
3
4
5
->
->
->
->
->
->
/dev/pts/0
/tmp/vmstat.out
/dev/pts/0
/proc/uptime
/proc/stat
/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.
Use the following transcript to answer the next 3 questions.
[prince@station prince]$ cal > cal.out
[prince@station prince]$ ls -l /dev/stdin
rha030-3.0-0-en-2005-08-17T07:23:17-0400
18
Copyright (c) 2003-2005 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 tollfree (USA) +1 866 626 2994 or +1 (919) 754 3700.
1 root
root
17 Apr
September 2003
Su Mo Tu We Th Fr
1 2 3 4 5
7 8 9 10 11 12
14 15 16 17 18 19
21 22 23 24 25 26
28 29 30
Sa
6
13
20
27
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.
19
rha030-3.0-0-en-2005-08-17T07:23:17-0400
Copyright (c) 2003-2005 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.
Unix programs commonly report error conditions to a destination called Standard Error (stderr).
Usually, stderr is connected to a terminals 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
Standard Error (stderr)
We have discussed the standard input and output streams, stdin and stdout, and how to use > and < in the
bash command line to redirect them. We are now ready to confuse matters a little by introducing a
second output stream, commonly used for reporting error conditions, called Standard Error (often
abbreviated stderr).
In the following sequence, elvis is using the head -1 command to generate a list of the first lines of all
the files in the /etc/rc.d directory.
[elvis@station elvis]$ ls -F /etc/rc.d/
init.d/
rc*
rc0.d/
rc1.d/
rc2.d/
rc3.d/
rc4.d/
rc5.d/
rc6.d/
rc.local*
rc.sysinit*
rc.sysinit.rpmsave*
20
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.
[elvis@station elvis]$ head -1 /etc/rc.d/ * > rcsummary.out
head:
head:
head:
head:
head:
head:
head:
head:
/etc/rc.d/init.d: Is a directory
/etc/rc.d/rc0.d: Is a directory
/etc/rc.d/rc1.d: Is a directory
/etc/rc.d/rc2.d: Is a directory
/etc/rc.d/rc3.d: Is a directory
/etc/rc.d/rc4.d: Is a directory
/etc/rc.d/rc5.d: Is a directory
/etc/rc.d/rc6.d: Is a directory
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.
[elvis@station elvis]$ head -1 /etc/rc.d/ * 2> rcsummary.err
rha030-3.0-0-en-2005-08-17T07:23:17-0400
21
Copyright (c) 2003-2005 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.
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 shouldnt be hard to guess.
[elvis@station elvis]$ cat rcsummary.err
head:
head:
head:
head:
head:
head:
head:
head:
/etc/rc.d/init.d: Is a directory
/etc/rc.d/rc0.d: Is a directory
/etc/rc.d/rc1.d: Is a directory
/etc/rc.d/rc2.d: Is a directory
/etc/rc.d/rc3.d: Is a directory
/etc/rc.d/rc4.d: Is a directory
/etc/rc.d/rc5.d: Is a directory
/etc/rc.d/rc6.d: Is a directory
In the following example, both > and 2> are used to redirect stdout and stderr independently.
[elvis@station elvis]$ head -1 /etc/rc.d/ * > rcsummary.out 2> rcsummary.err
[elvis@station elvis]$
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-3.0-0-en-2005-08-17T07:23:17-0400
22
Copyright (c) 2003-2005 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.
head: /etc/rc.d/init.d: I
==> /etc/rc.dhead: /etc/rc.d/rc0.d: Is a directory
head: /etc/rc.d/rc1.d: Is a direc
==> head: /etc/rc.d/rc2.
==> /etc/rc.d/rc3head: /
==> /etc/rc.d/rc4.d <==
==> /etc/rc.d/rc5.d <==
==> /etc/rc.d/rc6.d <==
==> /etc/rc.d/rc.local <==
#!/bin/sh
==> /etc/rc.d/rc.sysinit <==
#!/bin/bash
==> /etc/rc.d/rc.sysinit.rpmsave <==
#!/bin/bash
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 others 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.
[elvis@station elvis]$ head -1 /etc/rc.d/ * > rcsummary.both 2>&1
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.
[elvis@station elvis]$ cat rcsummary.both
rha030-3.0-0-en-2005-08-17T07:23:17-0400
23
Copyright (c) 2003-2005 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.
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.
Table 2-1. Redirecting stdin, stdout, and stderr in bash
syntax
effect
Combine stdout and stderr, and redirect both into file. (Portable syntax)
Combine stdout and stderr, and redirect both into file. (Convenient syntax)
Examples
Example 1. Using /dev/null to filter out stderr
The user elvis is has recently learned that, besides the /home/elvis and /tmp directories hes familiar
with, he may also own files in the /var directory. These files are usually spooling files for received but
not yet viewed email, print jobs waiting to be sent to the printer, etc.
Curious, he uses the find command to find all files within the /var directory that he owns.
[elvis@station elvis]$ find /var -user elvis
find:
find:
find:
...
find:
find:
rha030-3.0-0-en-2005-08-17T07:23:17-0400
24
Copyright (c) 2003-2005 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.
(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.
[elvis@station elvis]$ find /var -user elvis 2> /tmp/foo
/var/spool/mail/elvis
While this works, elvis is left with a file called /tmp/foo that he really didnt 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.
[elvis@station elvis]$ find /var -user elvis 2> /dev/null
/var/spool/mail/elvis
As the following long listing shows, /dev/null is a character device node, like those used for
conventional device drivers.
[elvis@station elvis]$ ls -l /dev/null
crw-rw-rw-
1 root
root
1,
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/*
25
rha030-3.0-0-en-2005-08-17T07:23:17-0400
Copyright (c) 2003-2005 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.
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.
[madonna@station madonna]$ cat chmod.err
chmod:
chmod:
chmod:
chmod:
chmod:
changing
changing
changing
changing
changing
permissions
permissions
permissions
permissions
permissions
of
of
of
of
of
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-3.0-0-en-2005-08-17T07:23:17-0400
26
Copyright (c) 2003-2005 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.
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
Use the following transcript to answer the next two questions.
[madonna@station madonna]$ cat /etc/t* > /tmp/cat.out 2> /tmp/cat.err
[1]+
Stopped
[madonna@station madonna]$ ps
PID
2419
3126
3127
TTY
pts/1
pts/1
pts/1
TIME
00:00:00
00:00:00
00:00:00
CMD
bash
cat
ps
total 4
lrwx-----l-wx-----l-wx-----lr-x------
1
1
1
1
madonna
madonna
madonna
madonna
madonna
madonna
madonna
madonna
64
64
64
64
Sep
Sep
Sep
Sep
23
23
23
23
04:15
04:15
04:15
04:15
0
1
2
3
->
->
->
->
/dev/pts/1
/tmp/cat.out
/tmp/cat.err
/etc/termcap
[madonna@station madonna]$ fg
[madonna@station madonna]$ ???????
27
rha030-3.0-0-en-2005-08-17T07:23:17-0400
Copyright (c) 2003-2005 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.
#!/bin/bash
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
28
rha030-3.0-0-en-2005-08-17T07:23:17-0400
Copyright (c) 2003-2005 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.
rha030-3.0-0-en-2005-08-17T07:23:17-0400
29
Copyright (c) 2003-2005 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.
Discussion
Pipes
In the previous Lessons, we have seen that a processs 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.
[prince@station prince]$ find /etc -size +100k 2>/dev/null
/etc/termcap
/etc/gconf/gconf.xml.defaults/schemas/desktop/gnome/interface/%gconf.xml
/etc/gconf/gconf.xml.defaults/schemas/apps/mailcheck_applet/prefs/%gconf.xml
/etc/gconf/gconf.xml.defaults/schemas/apps/tasklist_applet/prefs/%gconf.xml
...
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.
[prince@station prince]$ find /etc -size +100k 2>/dev/null | sort
/etc/aep/aeptarg.bin
/etc/gconf/gconf.xml.defaults/schemas/apps/gedit-2/preferences/editor/save/%gconf.xml
/etc/gconf/gconf.xml.defaults/schemas/apps/gnomemeeting/general/%gconf.xml
30
Chapter 3. Pipes
...
/etc/makedev.d/cciss
/etc/makedev.d/dac960
/etc/squid/squid.conf
/etc/squid/squid.conf.default
/etc/termcap
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
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. Hes trying to find a directory called templates that he created a few months ago. He uses the
locate command to help him find it.
[prince@station prince]$ locate templates
/etc/openldap/ldaptemplates.conf
/usr/share/doc/libxslt-1.0.27/html/libxslt-templates.html
/usr/share/doc/libxslt-1.0.27/templates.gif
/usr/share/doc/docbook-style-xsl-1.58.1/docsrc/templates.xml
/usr/share/man/man5/ldaptemplates.conf.5.gz
/usr/share/man/man3/ldap_free_templates.3.gz
/usr/share/man/man3/ldap_init_templates_buf.3.gz
/usr/share/man/man3/ldap_init_templates.3.gz
...
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".
[prince@station 010_section_discussion]$ locate templates | grep prince
/home/prince/.kde/share/apps/quanta/templates
/home/prince/proj/templates
rha030-3.0-0-en-2005-08-17T07:23:17-0400
31
Copyright (c) 2003-2005 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".
[prince@station prince]$ grep prince /etc/ * | grep 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-3.0-0-en-2005-08-17T07:23:17-0400
32
Copyright (c) 2003-2005 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
Example 1. Listing Processes by Name
Often, one would like to list information about processes which are running a specific command. While
ps aux tables a lot of information about currently running processes, the number of processes running on
the machine can make the output overwhelming. The grep command can help simplify the output.
In the following, prince would like to list information about the processes which are implementing his
web server, the httpd command. He lists all processes, but then reduces the output to only those lines
which contain the text httpd.
[prince@station prince]$ ps aux | grep httpd
root
apache
apache
apache
apache
apache
apache
prince
889
907
913
914
1979
1980
1981
4905
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.5
0.7
0.5
0.5
0.8
0.5
0.2
18248
18436
18436
18436
18360
18388
18360
3572
100
1320
1952
1332
1524
2140
1524
640
?
?
?
?
?
?
?
pts/1
S
S
S
S
S
S
S
S
Sep22
Sep22
Sep22
Sep22
Sep22
Sep22
Sep22
06:19
0:00
0:00
0:00
0:00
0:00
0:00
0:00
0:00
/usr/sbin/httpd
/usr/sbin/httpd
/usr/sbin/httpd
/usr/sbin/httpd
/usr/sbin/httpd
/usr/sbin/httpd
/usr/sbin/httpd
grep httpd
102
175
434
675
680
682
683
690
rha030-3.0-0-en-2005-08-17T07:23:17-0400
33
Copyright (c) 2003-2005 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.
[prince@station prince]$ !680
find
728
132
304
...
27
27
27
27
27
27
27
27
12:18:21
12:18:27
12:19:15
12:19:21
12:19:21
12:20:27
12:20:27
12:20:27
server1
server1
server1
server1
server1
server1
server1
server1
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.
[root@station log]# grep DHCPOFFER messages
rha030-3.0-0-en-2005-08-17T07:23:17-0400
34
Copyright (c) 2003-2005 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
May 27 11:46:30 server1 dhcpd: DHCPOFFER on 192.168.0.1 to 00:08:74:d9:41:9e via eth0
May 27 11:48:40 server1 dhcpd: DHCPOFFER on 192.168.0.2 to 00:08:74:d9:41:32 via eth0
...
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.
[root@station log]# grep DHCPOFFER messages | sed "s/^. *on //"
192.168.0.1
192.168.0.1
192.168.0.1
192.168.0.1
192.168.0.2
192.168.0.2
192.168.0.2
192.168.0.2
192.168.0.3
...
to
to
to
to
to
to
to
to
to
00:08:74:d9:41:9e
00:08:74:d9:41:9e
00:08:74:d9:41:9e
00:08:74:d9:41:9e
00:08:74:d9:41:32
00:08:74:d9:41:32
00:08:74:d9:41:32
00:08:74:d9:41:32
00:08:74:d9:40:a4
via
via
via
via
via
via
via
via
via
eth0
eth0
eth0
eth0
eth0
eth0
eth0
eth0
eth0
If you are not familiar with the sed command (and you probably arent), dont worry about the details.
Just note that seds argument had the effect of removing the leading text from each line, up to the word
"on". Theres 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.
[root@station log]$ grep DHCPOFFER messages | sed "s/^. *on //
" | awk {print $1,$3}
...
192.168.0.14 00:08:74:34:fe:bc
192.168.0.5 00:08:74:34:fd:36
192.168.0.15 00:08:74:37:c8:eb
192.168.0.15 00:08:74:37:c8:eb
192.168.0.6 00:08:74:d9:41:a3
192.168.0.16 00:08:74:d9:41:ac
192.168.0.7 00:08:74:d9:41:53
192.168.0.16 00:08:74:d9:41:ac
192.168.0.17 00:08:74:35:00:e3
...
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.
[root@station log]$ grep DHCPOFFER messages | sed "s/^. *on //
" | awk {print $1,$3} | sort | uniq
192.168.0.10 00:08:74:d9:40:95
192.168.0.1 00:08:74:d9:41:9e
192.168.0.110 00:09:6b:d0:ce:8f
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
rha030-3.0-0-en-2005-08-17T07:23:17-0400
35
Copyright (c) 2003-2005 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.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.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 didnt work quite right. The
information is sorted, but its 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 administrators
mental toolkit: grep, sed, awk, sort, and uniq.
If the administrator were using an operating system that wasnt 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-3.0-0-en-2005-08-17T07:23:17-0400
36
Copyright (c) 2003-2005 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.
37
rha030-3.0-0-en-2005-08-17T07:23:17-0400
Copyright (c) 2003-2005 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
Questions
1. Which of the following command lines would reduce the output of the locate conf command to only files whose
name or path contains the text python?
( ) a. locate conf >> grep python
( ) b. locate conf | grep python
( ) c. locate conf < grep python
( ) d. locate conf | grep < python
( ) e. None of the above
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
Use the following transcript to answer the next 6 questions.
[elvis@station elvis]$ ls -R / 2> /dev/null | grep etc | less
[1]+
Stopped
[elvis@station elvis]$ ps
PID
1603
2391
2392
2393
2394
TTY
pts/2
pts/2
pts/2
pts/2
pts/2
TIME
00:00:01
00:00:00
00:00:00
00:00:00
00:00:00
CMD
bash
ls
grep
less
ps
total 0
lrwx-----l-wx-----l-wx------
1 elvis
1 elvis
1 elvis
elvis
elvis
elvis
38
rha030-3.0-0-en-2005-08-17T07:23:17-0400
Copyright (c) 2003-2005 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
( ) e. None of the above
4. Which file (or pipe) is tied to stderr of the grep process?
( ) a. /dev/null
( ) b. /dev/pts/2
( ) c. A pipe to the ls process
( ) d. A pipe to the less process
( ) e. None of the above
5. Which file (or pipe) is tied to stdin of the grep process?
( ) a. A pipe to the less process
( ) b. A pipe to the ls process
( ) c. /dev/pts/2
( ) d. /dev/null
( ) e. None of the above
6. Which file (or pipe) is tied to stdin of the less process?
( ) a. A pipe to the ls process
( ) b. /dev/pts/2
( ) c. /dev/null
( ) d. A pipe to the grep process
( ) e. None of the above
7. To what expression would the symbolic link /proc/2392/fd/0 resolve?
( ) a. /dev/pts/2
( ) b. pipe:[20966]
( ) c. /dev/null
( ) d. /tmp
( ) e. /dev/tty2
39
rha030-3.0-0-en-2005-08-17T07:23:17-0400
Copyright (c) 2003-2005 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.
( ) 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 greps 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.
[elvis@station elvis]$ ls -R / | grep etc | less
[1]+
Stopped
[elvis@station elvis]$ ps -j
PID
1215
3242
3243
3244
3246
PGID
1215
3242
3242
3242
3246
SID
1215
1215
1215
1215
1215
TTY
pts/0
pts/0
pts/0
pts/0
pts/0
TIME
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
CMD
bash
ls
grep
less
ps
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-3.0-0-en-2005-08-17T07:23:17-0400
40
Copyright (c) 2003-2005 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.