6.1 Standard Input and Standard Output


What happens if you don't give a filename argument on a command line? Most programs will take their input from your keyboard instead (after you press Return to start the program running, that is). Your Terminal keyboard is the program's standard input .

As a program runs, the results are usually displayed on your Terminal screen. The Terminal screen is the program's standard output . So, by default, each of these programs takes its information from the standard input and sends the results to the standard output. These two default cases of input/ output (I/O) can be varied. This is called I/O redirection .

If a program writes to its standard output, which is normally the screen, you can make it write to a file instead by using the greater-than symbol ( > ) operator. The pipe operator ( ) sends the standard output of one program to the standard input of another program. Input/output redirection is one of the most powerful and flexible Unix features.

If a program doesn't normally read from files, but reads from its standard input, you can give a filename by using the less-than symbol ( < ) operator. tr (character translator) is such a program. Here's how to use the input redirection operator to convert commas to linefeeds in the to_do file:

 $  cat to_do  Install Mac OS X,Learn Unix,???,Profit! $  tr ',' '\n' < to_do  Install Mac OS X Learn Unix ??? Profit! $ 

Can you see what's happened here? The tr command has translated every comma in the input file ( to_do , which replaced standard input because of the < notation) to a carriage return, displaying the output on standard output (the Terminal window).

6.1.1 Putting Text in a File

Instead of always letting a program's output come to the screen, you can redirect output to a file. This is useful when you'd like to save program output or when you put files together to make a bigger file.

6.1.1.1 cat

cat , which is short for "concatenate," reads files and outputs their contents one after another, without stopping.

To display files on the standard output (your screen), use:

 cat   file(s)   

For example, let's display the contents of the file /etc/bashrc . This system file is the global login file for bassh :

 $  cat /etc/bashrc  # System-wide .bashrc file for interactive bash(1) shells. PS1='\h:\w \u$ ' $ 

You cannot go back to view the previous screens, as you can when you use a pager program such as less (unless you're using a Terminal window with a sufficient scrollback buffer, that is). cat is mainly used with redirection, as we'll see in a moment.

By the way, if you enter cat without a filename, it tries to read from the keyboard (as we mentioned earlier). You can get out by pressing Control-D.

When you add > filename to the end of a command line, the program's output is diverted from the standard output to a file. The > symbol is called the output redirection operator .

When you use the > operator, be careful not to accidentally overwrite a file's contents. Your system may let you redirect output to an existing file. If so, the old file's contents will be lost forever (or, in Unix lingo, "clobbered"). Be careful not to overwrite a much needed file!

Many shells can protect you from the risk. In bash (the default shell for Mac OS X), use the command set noclobber . Enter the command at a shell prompt or put it in your ~/.profile file. After that, the shell won't allow you to redirect onto an existing file and overwrite its contents.

This doesn't protect against overwriting by Unix programs such as cp ; it works only with the > redirection operator. For more protection, you can set Unix file access permissions (see Chapter 4).

For example, let's use cat with the output redirection operator. The file contents that you'd normally see on the screen (from the standard output) are diverted into another file, which we'll then read using cat (without any redirection!):

  $ cat /etc/bashrc > mybashrc   $ cat mybashrc  # System-wide .bashrc file for interactive bash(1) shells. PS1='\h:\w \u$ ' $ 

An earlier example showed how cat /etc/bashrc displays the file /etc/bashrc on the screen. The example here adds the > operator, so the output of cat goes to a file called mybashrc in the working directory. Displaying the file mybashrc shows that its contents are the same as the file /etc/bashrc (the effect is the same as the copy command cp /etc/bashrc mybashrc ).

You can use the > redirection operator with any program that sends text to its standard output ”not just with cat . For example:

 $  who > users  $  date > today  $  ls  mylogin   today   users   ... 

We've sent the output of who to a file called users and the output of date to the file named today . Listing the directory shows the two new files. Let's look at the output from the who and date programs by reading these two files with cat :

 $  cat users  taylor   console  Sep 24 07:58  taylor   ttyp1    Sep 24 08:00  $  cat today  Wed Sep 24 09:41:07 MDT 2003  $ 

You can also use the cat program and the > operator to make a small text file. We told you earlier to type Control-D if you accidentally enter cat without a filename. This is because the cat program alone takes whatever you type on the keyboard as input. Thus, the command:

 cat >   filename   

takes input from the keyboard and redirects it to a file. Try the following example:

 $  cat > to_do  Finish report by noon Lunch with Xian Swim at 5:30 ^D $ 

cat takes the text that you typed as input (in this example, the three lines that begin with Finish , Lunch , and Swim ), and the > operator redirects it to a file called to_do . Type Control-D once , on a new line by itself, to signal the end of the text. You should get a shell prompt.

You can also create a bigger file from smaller files with the cat command and the > operator. The form:

 cat   file1 file2 > newfile   

creates a file newfile , consisting of file1 followed by file2 .

 $  cat today to_do > diary  $  cat diary  Wed Sep 24 09:41:07 MDT 2003 Finish report by noon Lunch with Xian Swim at 5:30 $ 

You shouldn't use redirection to add a file to itself, along with other files. For example, you might hope that the following command would merge today's to-do list with tomorrow's. This isn't a good idea.

 $  cat to_do to_do.tomorrow > to_do.tomorrow  

It works, but it runs for all eternity because it keeps copying the file over itself. If you cancel it with Control-C, and use ls to examine the file, you'll see that it's gotten quite large:

 ^C $  ls -sk to_do.tomorrow  81704 to_do.tomorrow 

ls -sk shows the size in kilobytes, so it grew to about 80 megabytes! The right way to do this is either to use a temporary file (as you'll see in a later example) or simply to use a text editor program.

You can add more text to the end of an existing file, instead of replacing its contents, by using the >> (append redirection) operator. Use it as you would the > (output redirection) operator. So, the following:

 cat   file2   >>   file1   

appends the contents of file2 to the end of file1 . This doesn't affect the contents of file2 : it is being read from, not written to.

For an example, let's append the contents of the file users and the current date and time to the file diary . Here's what it looks like:

 $  cat users >> diary  $  date >> diary  $  cat diary  Wed Sep 24 09:41:07 MDT 2003 Finish report by noon Lunch with Xian Swim at 5:30 taylor   console  Nov 11 22:06  taylor   ttyp1    Nov 15 08:16  Wed Sep 24 11:21:35 MDT 2003 $ 

Unix doesn't have a redirection operator that adds text to the beginning of a file. You can do this by storing the new text in a temporary file, then using a text editor program to read the temporary file into the start of the file you want to edit. You also can do the job with a temporary file and redirection. Maybe you'd like each day's entry to go at the beginning of your diary file. Simply rename diary to something like temp . Make a new diary file with today's entries, then append temp (with its old contents) to the new diary . For example:

 $  mv diary temp  $  date > diary  $  cat users >> diary  $  cat temp >> diary  $  rm temp  

This example could be shortened by combining the two cat commands into one, giving both filenames as arguments to a single cat command. That wouldn't work, though, if you were making a real diary with a command other than cat users .



Learning Unix for Mac OS X Panther
Learning Unix for Mac OS X Panther
ISBN: 0596006179
EAN: 2147483647
Year: 2003
Pages: 88

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net