I am trying to redirect both STDOUT/STDERR of a UNIX command and append to a log file in a korn shell.
我试图重定向UNIX命令的STDOUT / STDERR并附加到korn shell中的日志文件。
rm -rf file1 >>& logfile
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?
这个命令会在ksh中运行还是一个典型的bash命令?用上述命令会对你造成什么伤害?
3 个解决方案
#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:
刚刚在Red Hat Linux 4的Korn shell中测试过。 no_fred包含:
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."
“>”实际上是1>,表示重定向文件描述符1(标准输出)。 “2>”重定向标准错误,因为标准错误是文件描述符2.“&1”表示“无论您正在使用文件描述符1做什么”。总而言之,这意味着“将标准输出转储到文件中,并将标准错误与其一起转储”。
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.
此方法的一个优点是错误消息出现在正确的位置。例如,对于无法编译的文件,编译器的错误消息将出现在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.
>>&构造可能会将命令的输出附加到日志文件中,并将其放在后台。我不确定它是否会对标准错误做任何事情。我刚刚咨询了Bolsky / Korn 1989,它甚至不在那里,所以也许其他人可以解析它的作用。
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.
更新:如果命令中有任何管道,则会在错误生成命令运行时首先显示早期阶段的标准错误。由于只有标准输出通过管道路由,所以当整个管道完成时,它将全部出现。
#2
You're trying to use csh syntax in ksh. See Jason's answer.
您正尝试在ksh中使用csh语法。见杰森的回答。
#3
For appending:
ls -ld . fred 1>> no_fred 2>&1
#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:
刚刚在Red Hat Linux 4的Korn shell中测试过。 no_fred包含:
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."
“>”实际上是1>,表示重定向文件描述符1(标准输出)。 “2>”重定向标准错误,因为标准错误是文件描述符2.“&1”表示“无论您正在使用文件描述符1做什么”。总而言之,这意味着“将标准输出转储到文件中,并将标准错误与其一起转储”。
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.
此方法的一个优点是错误消息出现在正确的位置。例如,对于无法编译的文件,编译器的错误消息将出现在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.
>>&构造可能会将命令的输出附加到日志文件中,并将其放在后台。我不确定它是否会对标准错误做任何事情。我刚刚咨询了Bolsky / Korn 1989,它甚至不在那里,所以也许其他人可以解析它的作用。
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.
更新:如果命令中有任何管道,则会在错误生成命令运行时首先显示早期阶段的标准错误。由于只有标准输出通过管道路由,所以当整个管道完成时,它将全部出现。
#2
You're trying to use csh syntax in ksh. See Jason's answer.
您正尝试在ksh中使用csh语法。见杰森的回答。
#3
For appending:
ls -ld . fred 1>> no_fred 2>&1