Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash Unix Shell文件描述符_Bash_Unix_Ksh_File Descriptor - Fatal编程技术网

Bash Unix Shell文件描述符

Bash Unix Shell文件描述符,bash,unix,ksh,file-descriptor,Bash,Unix,Ksh,File Descriptor,我需要运行一个名为pg.sh的程序。它将标准输出报告到输出日志。如何将stdout以及stderr和stdout保存到两个单独的日志文件中 我搜索并得到了下面的代码 (pg.sh 2>&1 1>&3 ) 3>&1 1>&2 | tee output.log) > final.log 2>&1 我知道1和2是指向stdout和stderr的文件描述符。3是指向标准输出的另一个文件描述符 上面的代码工作得很好,但我不明白这

我需要运行一个名为pg.sh的程序。它将标准输出报告到输出日志。如何将stdout以及stderr和stdout保存到两个单独的日志文件中

我搜索并得到了下面的代码

(pg.sh 2>&1 1>&3 ) 3>&1 1>&2 | tee output.log) > final.log 2>&1
我知道1和2是指向stdout和stderr的文件描述符。3是指向标准输出的另一个文件描述符


上面的代码工作得很好,但我不明白这是如何实现的。有人能帮我写代码吗?

从外部重定向开始:
(..)3>&1>&2
,顺序很重要:

  • FD3作为1的副本打开写入(标准输出:这里是管道输入)
  • 然后fd 1被粉碎为2的副本(stderr)(或重定向到stderr)
然后,
|
输入是fd 3,而标准差不是由tee捕获的

嵌套重定向:

  • fd 2被重定向到标准输出(被重定向到外部标准输出)

  • fd 1被重定向到fd 3(被重定向到外部标准输出)

    当tee复制记录的输出时,(最终重定向
    >final.log 2>&1
    ,当fd 1之后fd 2打开时,它们都被重定向到final.log) 文件final.log将包含程序stdout和stderr,但output.log仅包含stdout

也许可以更容易地编写,使用
3>&11>&22>&3
,它可以反转stdout和stderr

以下各项也应这样做:

( pg.sh | tee output.log ) >final.log 2>&1
下面的代码将程序stdout写入output.log stderr到error.log,并将两者都写入final.log

( ( pg.sh | tee output.log ) 3>&1 1>&2 2>&3 | tee error.log ) >final.log 2>&1

从外部重定向开始:
(..)3>&1>&2
,顺序很重要:

  • FD3作为1的副本打开写入(标准输出:这里是管道输入)
  • 然后fd 1被粉碎为2的副本(stderr)(或重定向到stderr)
然后,
|
输入是fd 3,而标准差不是由tee捕获的

嵌套重定向:

  • fd 2被重定向到标准输出(被重定向到外部标准输出)

  • fd 1被重定向到fd 3(被重定向到外部标准输出)

    当tee复制记录的输出时,(最终重定向
    >final.log 2>&1
    ,当fd 1之后fd 2打开时,它们都被重定向到final.log) 文件final.log将包含程序stdout和stderr,但output.log仅包含stdout

也许可以更容易地编写,使用
3>&11>&22>&3
,它可以反转stdout和stderr

以下各项也应这样做:

( pg.sh | tee output.log ) >final.log 2>&1
下面的代码将程序stdout写入output.log stderr到error.log,并将两者都写入final.log

( ( pg.sh | tee output.log ) 3>&1 1>&2 2>&3 | tee error.log ) >final.log 2>&1

括号在代码摘录中不平衡,请编辑您的问题好吗?括号在代码摘录中不平衡,请编辑您的问题好吗?