UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 13.3 Send (only) Standard Error Down a Pipe Chapter 13
Redirecting Input and Output
Next: 13.5 Redirection in C Shell: Capture Errors, Too?
 

13.4 Problems Piping to a Pager

If your window onto UNIX (terminal, X window, communications program, whatever) doesn't have a way to show you the previous screenful, using a pager program like more (25.3), pg, or less (25.4) can be mighty handy. But piping to a pager doesn't always work the way you want it to.

Here's a grep command line that searches several files. What's wrong with it?

% grep "^set" */.cshrc | more

That wasn't a fair question because you can't tell what's wrong. The problem (it turns out) is that the files named barney/.cshrc, edie/.cshrc, and gail/.cshrc are read-protected (22.2). But, as the first part of Figure 13.1 shows, the error messages scroll off your screen and the pager doesn't stop them.

Figure 13.1: Standard Error Bypassing Pipe, Going Through Pipe

Figure 13.1

Unless your display is reallllly sloooowww, the error messages are lost and you never know they were there. Or the errors are jumbled up with the "good" grep output. That's because you've told the shell to send only the standard output of grep to the pager program. And grep writes its errors to the standard error ! (45.20) But both stdout and stderr go to the screen at once. The errors on stderr scroll away with the output from the pager. The pager can't count the lines of errors so it outputs a complete screenful of stdout (the "good stuff"). If grep's standard output (from the files it could read) is at least a screenful, as it is here, there are too many lines to fit on the screen-and some lines will scroll off.

The better way to do this is to combine grep's stdout and stderr and give them both to the pager. These command lines (in csh and sh) both do that:

% grep "^set" */.cshrc |& more $ grep "^set" */.cshrc 2>&1 | more

The second part of Figure 13.1 shows how this works. Any time I pipe a command's output to a pager, I usually combine the stdout and stderr this way.

- JP


Previous: 13.3 Send (only) Standard Error Down a Pipe UNIX Power ToolsNext: 13.5 Redirection in C Shell: Capture Errors, Too?
13.3 Send (only) Standard Error Down a Pipe Book Index13.5 Redirection in C Shell: Capture Errors, Too?

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System