Will this redirection ">>& <filename>" work in Korn shell?

Will this redirection ">>& <filename>" work in Korn shell?

凌乱心跳 发布于 2021-11-27 字数 291 浏览 661 回复 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?

如果你对这篇文章有疑问,欢迎到本站 社区 发帖提问或使用手Q扫描下方二维码加群参与讨论,获取更多帮助。

扫码加入群聊

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

七婞 2022-06-07 3 楼

For appending:

ls -ld . fred 1>> no_fred 2>&1 
德意的啸 2022-06-07 2 楼

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

长发绾君心 2022-06-07 1 楼

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.