views:

1030

answers:

3

I am trying to redirect both STDOUT/STDERR of a UNIX command and append to a log file in a korn shell.

rm -rf file1 >>& logfile

Will this command work in ksh or is this a typical bash command? What harm would I be causing with above command?

+4  A: 

I use this form to redirect standard output and standard error to the same file.

ls -ld . fred > no_fred 2>&1

Just tested in Red Hat Linux 4's Korn shell. no_fred contains:

ls: fred: No such file or directory
drwxrwxr-x  2 user group 1024 Apr 27 17:41 .

">" is actually 1>, which says to redirect file descriptor 1 (standard output). "2>" redirects standard error, since standard error is file descriptor 2. "&1" means "whatever you're doing with file descriptor 1". So all together, this means "dump standard output into a file, and standard error along with it."

One advantage of this method is that error messages appear in the right place. For example, a compiler's error messages, for a file which failed to compile, will appear right after the compilation command from your makefile.

The >>& construct might append the output of the command to the log file, and puts this in the background. I'm not sure it does anything with standard error. I just consulted Bolsky/Korn 1989, and it's not even in there, so maybe someone else can parse what it does.

Update: If you have any pipes in your command, then the standard error of early stages will appear first, as the error-producing command runs. Since only the standard output is routed through the pipe, it will all appear at once when the entire pipeline completes.

Jason Catena
+2  A: 

You're trying to use csh syntax in ksh. See Jason's answer.

John M
A: 

For appending:

ls -ld . fred 1>> no_fred 2>&1

Donald