Shell功能`>(tee tee copyError.txt>&2)的名称是什么?


11

我需要将stdout和stderr登录到日志文件,但只在屏幕上显示错误消息。我可以这样做:

cp -rpv a/* b 1> copyLog.txt 2> >(tee copyError.txt >&2) 

这是我在网络上找到的。

我只想知道这>(tee copyError.txt >&2)东西叫什么?我无法使用Google搜索,因为Google会忽略尖括号和括号之类的字符。



@terdon我认为OP已声明他想将StdOut和StdErr记录到文件中,但仍在屏幕上显示StdErr
Dmitry Avtonomov

Answers:


11

来自man bash

   Process Substitution
       Process substitution is supported  on  systems  that  support
       named  pipes  (FIFOs)  or  the  /dev/fd method of naming open
       files.  It takes the form of <(list) or >(list).  The process
       list  is  run with its input or output connected to a FIFO or
       some file in /dev/fd.  The name of this file is passed as  an
       argument  to  the current command as the result of the expan
       sion.  If the >(list) form is used, writing to the file  will
       provide  input  for  list.   If the <(list) form is used, the
       file passed as an argument should be read to obtain the  out
       put of list.

您可以通过按/然后输入搜索字符串来搜索手册页,这是查找此类信息的好方法。当然,这确实需要您知道要搜索的手册页:)

您必须引用(尽管,因为它在搜索时具有特殊含义。要在bash手册页中找到相关部分,请输入/>\(


我做了很多bash,所以我做了一个man bash> bashman.txt并将其设置为只读。现在,我可以将bashman.txt加载到文本编辑器中的另一个窗口中(只读),并使用该编辑器的所有功能进行搜索,复制和粘贴。

8

>(tee copyError.txt >&2) 实际上是几个不同的功能:

  • >(...)称为“流程替代”。它在其中创建一个命名管道文件/dev/fd,写入该文件将为括号中的过程提供输入。

  • >:通常,这称为“输出重定向”,可让您将标准输出(>1>)或标准错误(2>)发送到文件或进程。 >&2输出重定向,但在这种情况下,输出tee被发送到标准错误(这是&2是,&1是标准输出)

  • 如果不使用>,括号(())将启动一个子外壳。括号中的正在运行的命令将生成另一个shell,该shell仅在运行这些命令的情况下存在。如果在子shell中声明变量,则可以看到其工作原理:

    $ foo='Tom';(foo='Dick'; echo "Sub: $foo"); echo "Orig: $foo"
    Sub: Dick
    Orig: Tom
    

    如您所见,$foo在子外壳程序中定义的与在父外壳程序中定义的是分开的。


4
>(...)不是重定向。>(...)扩展为文件名。如果要将输出重定向到该地址,则需要> >(...)>(...)在无法使用重定向的地方更常用。OP的命令可以使用传统管道来实现,而无需在那里进行流程替代。
斯特凡Chazelas

@StephaneChazelas很高兴看到您想到的解决方案
Dmitry Avtonomov 2013年

3
@chhh,cmd 2>&1 > output | tee err >&2
斯特凡Chazelas

@StephaneChazelas thx!
德米特里·阿夫托诺莫夫,2013年

这个答案的前半部分是错误的,或者至少是高度误导的;确实>可以表示输出重定向,也()可以表示子外壳,但>(...)实际上是一个单一的单一功能,不包含>()
ruakh
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.