重新定向的脚本,其输出重新定向内命令的输出 [英] Re-direct output of command within a script whose output is re-directed

查看:109
本文介绍了重新定向的脚本,其输出重新定向内命令的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我的一个脚本调用是这样的:

Suppose that a script of mine is invoked like this:

(script.sh 1>&2) 2>err

有没有办法再直接由脚本运行的标准输出的其中一个命令的输出?我试着做 2 - ;&放大器; 1 该命令,但这并没有帮助。这回答提出了窗口命令外壳和解决方案重新定向到一个文件,而不是标准输出。

Is there a way to re-direct the output of one of the commands run by the script to standard output? I tried to do 2>&1 for that command, but that did not help. This answer suggests a solution for Windows command shell and re-directs to a file instead of the standard output.

对于一个简单的例子,假设脚本是:

For a simple example, suppose that the script is:

#!/bin/sh
# ... many commands whose output will go to `stderr`
echo aaa # command whose output needs to go to `stdout`; tried 2>&1  
# ... many commands whose output will go to `stderr`

我如何引起的输出回声进入标准输出(的一个迹象将是它会出现在屏幕上)时,如上图所示的脚本调用?

How do I cause the output of that echo to go to stdout (a sign of that would be that it would appear on the screen) when the script is invoked as shown above?

推荐答案

发送它在脚本到stderr

Send it to stderr in the script

echo this goes to stderr
echo so does this
echo this will end up in stdout >&2
echo more stderr

运行方式

 { ./script.sh 3>&2 2>&1 1>&3 ; } 2>err

ERR包含

this goes to stderr
so does this
more stderr

输出到标准输出

this will end up in stdout

这篇关于重新定向的脚本,其输出重新定向内命令的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆