Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.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:在输出到stdout时对stdin求值_Bash - Fatal编程技术网

bash:在输出到stdout时对stdin求值

bash:在输出到stdout时对stdin求值,bash,Bash,在bash中,我希望能够或多或少地“带外”分析stdin,同时复制到stdout,而无需通过tmp文件、变量或显式命名的fifo将其移动 两个类似的例子: while read foo; do somefunc $foo; echo "$foo"; done tee >(grep -qs bar && do_something_but_i_am_trapped_in_a_process_substitution_shell) 一行接一行不是世界末日,但我更喜欢更干净的东

在bash中,我希望能够或多或少地“带外”分析stdin,同时复制到stdout,而无需通过tmp文件、变量或显式命名的fifo将其移动

两个类似的例子:

while read foo; do somefunc $foo; echo "$foo"; done

tee >(grep -qs bar && do_something_but_i_am_trapped_in_a_process_substitution_shell)
一行接一行不是世界末日,但我更喜欢更干净的东西

我希望能够使用exec、文件描述符重定向和tee,这样我就可以执行以下操作:

hasABar=$(grep -qs bar <file descriptor magic> && echo yes || echo no)
hasABar=$(grep-qs-bar&&echo-yes | | echo-no)
。。。然后根据我是否有一个“bar”来做一些事情,但最后,stdout仍然是stdin的副本

更新:根据库格曼下面的回答,我适应了下面的内容,这两种方法都有效

(
    exec 3>&1
    myVar=$(tee /dev/fd/3 | grep -qs bar && echo yes || echo no) 
    #myVar=$(grep -qs bar <(tee /dev/fd/3) && echo yes || echo no)
    echo "$myVar" > /tmp/out1
)
(
执行3>&1
myVar=$(tee/dev/fd/3 | grep-qs-bar&&echo yes | | echo no)
#myVar=$(grep-qs-bar/tmp/out1
)

您可以将标准输出复制到fd 3,然后使用
tee
同时写入标准输出和fd 3

exec 3>&1
tee /dev/fd/3 | grep -qs bar
这是一个实际的例子。我把我键入的行加粗了

$ cat test
#!/bin/bash
exec 3>&1
tee /dev/fd/3 | grep bar >&2

$ ./test | wc
foo
bar
bar
foo
^D
      3       3      12
$cat测试
#!/bin/bash
执行3>&1
tee/dev/fd/3 | grep bar>&2
美元/测试| wc
福
酒吧
酒吧
福
^D
3 12

查看
grep bar
wc
如何处理我的输入?
grep
在我键入字符串“bar”时找到了它,并且
wc
计算了我键入的所有内容。

我如何捕获它而不是去stderr?啊,谢谢..我想我已经从你提到的内容中获得了它