home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Complete Idiot's Guide to Linux
(Publisher: Macmillan Computer Publishing)
Author(s): Manuel Ricart
ISBN: 078971826x
Publication Date: 12/22/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


STDOUT

Most of the programs you have used so far send their results to the standard output stream, STDOUT, which is usually “wired” to your console. Any time that you see something printed on the console (your screen), more than likely it was sent to the STDOUT stream.

Let’s experiment a little with redirecting STDOUT to a file.

Redirecting to a File: >

To redirect output to a file, you use the greater than symbol (>) and provide a filename to receive the output:

     [alberto@digital alberto]$ ls -l >      /tmp/listing
     [alberto@digital alberto]$ cat /tmp/listing
     total 2
     -rw-rw-r--   1 alberto  alberto       388 Jul 29 19:03 Xrootenv.0
     -rw-rw-r--   1 alberto  alberto         0 Aug  5 20:16 file
     drwxrwsr-x   2 alberto  groupfil     1024 Aug  4 22:13 groupfiles

In this example I did just that. I asked for a listing of my files, but instead of getting the listing displayed on my screen, I redirected the output of the ls command to the file /tmp/listing. To prove that it worked, I then sent the contents of the /tmp/listing file to my screen using the cat command you learned in Chapter 12, “Working with Files on the Shell.” This technique is useful for creating a list of the files in a directory.


Check This Out:  Safety First! Avoiding Clobbered Files: noclobber
Redirecting streams has one dangerous side effect: It can kill files. When redirecting output to files, if the file doesn’t exist, Linux creates it automatically. This benefit also presents a problem. What happens if the file already exists? The existing file is overwritten automatically. This means that the shell replaces the existing file’s data with the new data—without prompting or even notifying you!

Most shells have the capability to set a noclobber variable. This variable tells the shell to reject redirections that would result in the destruction of a file and to reject appending to files when they don’t exist.

When working with shells, enabling this functionality can be a good idea, because it makes it less likely that the shell will have some side effect you didn’t intend.

To enable noclobber, type the following on your bash shell:

noclobber=1

If you are using a different shell, like the C shell, you might want to try this:

set noclobber=1

To test if the command took, type the following:

[alberto@digital alberto]$ noclobber=1

[alberto@digital alberto]$ touch file

[alberto@digital alberto]$ date > file

bash: file: Cannot clobber existing file

If you get the same error, noclobber is set properly. I tried to redirect the output of a command called date to file. Because file already existed, such redirection would have caused the contents of file to be replaced by whatever date printed. Such action is not allowed when you have noclobber defined in your environment. (The date command is covered in Chapter 17, “Command Toolbox: Useful Shell Commands and Shortcuts.”)


Now that you know about redirection, let me show you a neat trick: Let’s use the cat command as a minimal text editor. We can do this by issuing the cat command and then redirecting the output to a file, like this:

     [alberto@digital alberto]$ cat > /tmp/message.txt
     This is a test. Until I type a Ctrl+D, I can type all that I want.
     I can even enter new lines.

     -- Alberto
     [alberto@digital alberto]$ cat /tmp/message.txt
     This is a test. Until I type a Ctrl+d, I can type all that I want.
     I can even enter new lines.

     -- Alberto
     [alberto@digital alberto]$

With redirection, you can use cat as a simple editor. You can edit the current line you are working on, as the shell won’t grab the characters until you hit Enter. To finish entering text, just press Ctrl+D.

Appending a File: >>

Redirecting is very useful, but sometimes you don’t want to replace the file’s previous contents. Instead, you might want to add information to the end of a file. Maybe you want to maintain a list of all your favorite Web sites. In such a case, adding a new entry should not replace the entire file but augment it.

Instead of using a single >, you use a >> to tell the shell to append new output instead of overwriting. If the file doesn’t exist, it is created, like this:

     [alberto@digital alberto]$ cat > /tmp/websites
     www.caldera.com
     www.redhat.com
     [alberto@digital alberto]$ cat >> /tmp/websites
     www.mcp.com
     [alberto@digital alberto]$ cat /tmp/websites
     www.caldera.com
     www.redhat.com
     www.mcp.com

Why couldn’t I have used append (>>) for the first cat? Because I enabled the noclobber feature, the >> command would have resulted in an error. Appending to files that don’t exist produces an error when noclobber is set. (An alternative would have been to issue a touch /tmp/websites before using cat.)


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.