Redirecting Standard Error

Both bash and tcsh use a greater than symbol (>) to redirect standard output, but tcsh does not use the bash notation 2> to redirect standard error. Under tcsh you use a greater than symbol followed by an ampersand (>&) to combine and redirect standard output and standard error. Although you can use this notation under bash, it is not common. The following examples, like the bash examples on page 263, reference file x, which does not exist, and file y, which contains a single line.

tcsh $ cat x cat: x: No such file or directory tcsh $ cat y This is y. tcsh $ cat x y >& hold tcsh $ cat hold cat: x: No such file or directory This is y. 

With an argument of y in the preceding example, cat sends a string to standard output. An argument of x causes cat to send an error message to standard error.

Unlike bash, tcsh does not provide a simple way to redirect standard error separately from standard output. A work-around frequently provides a reasonable solution. The following example runs cat with arguments of x and y in a subshell (the parentheses ensure that the command within them runs in a subshellsee page 271). Also within the subshell a > redirects standard output to the file outfile. Output sent to standard error is not touched by the subshell but rather is sent to the parent shell, where both it and standard output are sent to errfile. Because standard output has already been redirected, errfile contains only output sent to standard error.

tcsh $ (cat x y > outfile) >& errfile tcsh $ cat outfile This is y. tcsh $ cat errfile cat: x: No such file or directory 

It can be useful to combine and redirect output when you want to run a slow command in the background and do not want its output cluttering up the terminal screen. For example, because the find utility (page 728) often takes some time to complete, it may be a good idea to run it in the background. The next command finds in the filesystem hierarchy all files that contain the string biblio in their name. The command runs in the background and sends its output to the findout file. Because the find utility sends to standard error a report of directories that you do not have permission to search, the findout file contains a record of any files that are found as well as a record of the directories that could not be searched.

tcsh $ find / -name "*biblio*" -print >& findout & 

In this example, if you did not combine standard error with standard output and redirected only standard output, the error messages would appear on the screen and findout would list only files that were found.

While a command that has its output redirected to a file is running in the background, you can look at the output by using tail (page 859) with the f option. The f option causes tail to display new lines as they are written to the file:

tcsh $ tail -f findout 

To terminate the tail command, press the interrupt key (usually CONTROL-C).

A Practical Guide to UNIX[r] for Mac OS[r] X Users
A Practical Guide to UNIX for Mac OS X Users
ISBN: 0131863339
EAN: 2147483647
Year: 2005
Pages: 234

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: