• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

使用bash,我怎么能管标准错误到另进程

用户头像
it1352
帮助1

问题说明

这是众所周知如何管一个进程的标准输出到另一个进程的标准输入:

It's well known how to pipe the standard ouput of a process into another processes standard input:

proc1 | proc2

但如果我要发送PROC1的标准误差PROC2离开标准输出将其当前位置?你可能会认为庆典将有沿行的命令:

proc1 2| proc2

但是,唉,没有。有没有办法做到这一点?

But, alas, no. Is there any way to do this?

正确答案

#1

您可以使用下面的技巧来的交换标准输出标准错误。然后你只需使用常规的管道功能。

You can use the following trick to swap stdout and stderr. Then you just use the regular pipe functionality.

( proc1 3>&1 1>&2- 2>&3- ) | proc2

提供的标准输出标准错误无论是在开始指向同一个地方,这会给你你需要什么

Provided stdout and stderr both pointed to the same place at the start, this will give you what you need.

它所做的:

    • 3&GT;&放大器; 1 创建它被设置为当前<$ C $一个新的文件句柄 3 C> 1 (原标准输出)刚刚保存在某个地方吧。
    • 1&GT;和2 - 套标准输出,以获得当前文件句柄 2 (原标准错误),然后关闭 2
    • 2 - ;及3 - 套标准错误,以得到当前文件句柄 3 (原标准输出),然后关闭 3
  • 3>&1 creates a new file handle 3 which is set to the current 1 (original stdout) just to save it somewhere.
  • 1>&2- sets stdout to got to the current file handle 2 (original stderr) then closes 2.
  • 2>&3- sets stderr to got to the current file handle 3 (original stdout) then closes 3.

这是有效的,你看到的排序swap命令:

It's effectively the swap command you see in sorting:

temp   = value1;
value1 = value2;
value2 = temp;

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcfefhj
系列文章
更多 icon
同类精品
更多 icon
继续加载