bash 如何在bash中重定向所有stderr?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 20:27:32  来源:igfitidea点击:

How to redirect all stderr in bash?

linuxbashshell

提问by Grégtheitroade Cachet

I'm looking for a way to redirect all the stderr streams in interactive bash (ideally to its calling parent process).

我正在寻找一种方法来重定向交互式 bash 中的所有 stderr 流(理想情况下重定向到其调用父进程)。

I don't want to redirect stderr stream from each individual command, which I could do by appending 2> a_fileto each command.

我不想从每个单独的命令重定向 stderr 流,我可以通过附加2> a_file到每个命令来做到这一点。

By default, these stderr streams are redirected to the stdout of an interactive bash. I would like to get them on the stderr of this interactive bash process in order to prevent my stdout to be polluted by error messages and be able to treat them separatly.

默认情况下,这些 stderr 流被重定向到交互式 bash 的 stdout。我想让它们进入这个交互式 bash 进程的 stderr,以防止我的 stdout 被错误消息污染并能够单独处理它们。

Any ideas?

有任何想法吗?

I still haven't found an answer ... But maybe it's actually a tty parameter. Does anybody knows something about tty/interactive shell responsibility for handling stderr ?

我仍然没有找到答案......但也许它实际上是一个 tty 参数。有人知道处理 stderr 的 tty/交互式 shell 责任吗?

回答by gavrie

Use the execbuiltin in bash:

使用exec内置的 bash:

exec 2> /tmp/myfile

exec 2> /tmp/myfile

回答by caerwyn

You could launch a new bash process redirecting the stderr of that process:

您可以启动一个新的 bash 进程来重定向该进程的 stderr:

  $ bash -i 2> stderr.log
  $ 

回答by Kit Gerrits

Two things:

两件事情:

  1. Using 2>&1in a remote ssh command results in the error ending up inside the local tarfile, resulting in a 'broken' backup.
  2. If you want to apply a redirect on the other side of the ssh, remember to escape the redirect command.
  1. 使用2>&1在本地tar文件内结束了该错误的远程ssh命令的结果,造成了“破”的备份。
  2. 如果要在 ssh 的另一端应用重定向,请记住对重定向命令进行转义。

My suggestion would be to redirect stderron the remote side to a file and pick it up later, in case of an error.

我的建议是stderr在远程端重定向到一个文件,然后在出现错误的情况下提取它。

example:

例子:

ssh -t remotehost tar -cf - /mnt/backup 2\>backup.err > localbackup.tar
EXITSTATUS=$?
if [ $EXITSTATUS != "0" ] then 
  echo Error occurred!
  ssh remotehost cat backup.err >localbackup.errors
  cat localbackup.errors
  ssh remotehost rm backup.err 
else 
  echo Backup completed successfully!
  ssh remotehost rm backup.err 
fi

回答by stefano

I find a good way is to surround the commands by parentheses, '()', (launch a sub-shell) or curly-braces, '{}' (no sub-shell; faster):

我发现一个好方法是用括号“()”、(启动子外壳)或花括号、“{}”(无子外壳;更快)将命令括起来:

{
  cmd1
  cmd2
  ...
  cmdN
} 2> error.log

Of course, this can be done on 1 line:

当然,这可以在 1 行上完成:

{ cmd1; cmd2; ... cmdN; } 2> error.log

回答by jtimberman

Try your commands in doublequotes, like so:

用双引号试试你的命令,像这样:

ssh remotehost "command" 2>~/stderr

Tested on my local system using a nonexistant file on the remote host.

使用远程主机上不存在的文件在我的本地系统上进行了测试。

$ ssh remotehost "tail x;head x" 2>~/stderr
$ cat stderr 
tail: cannot open `x' for reading: No such file or directory
head: cannot open `x' for reading: No such file or directory

回答by Gunstick

I don't see your problem it works as designed:

我没有看到你的问题它按设计工作:

$ ssh remotehost 'ls nosuchfile; ls /etc/passwd' >/tmp/stdout 2>/tmp/stderr 
$ cat /tmp/stdout  
/etc/passwd 
$ cat /tmp/stderr 
nosuchfile not found

回答by crb

Tried ssh -tto create a pseudo-TTY at the remote end?

试图ssh -t在远端创建一个伪 TTY?