如何使用bash将stdout传递给脚本并写入终端?

时间:2017-08-03 21:51:21

标签: linux bash shell

我想将作业的输出传递给脚本以读入该stdout行并完成操作并在终端上显示输出。

现在,我有这个......

ls | ./script.sh

这允许我的脚本在输出上运行,但不会在终端上显示ls的结果。

我试过这个:

ls | tee ./script.sh

但是这个用ls的输出覆盖了script.sh的内容

如何在终端上显示“ls”的输出,并在该输入上运行script.sh上的内容?以下是我的script.sh的示例:

#!/bin/bash
while read line
  do
    echo line input
  done

1 个答案:

答案 0 :(得分:6)

你可以这样做:

 ls | tee /dev/tty | ./script.sh

或者,如果你想在管道之前使用什么stdout,你可以做到 类似的东西:

{ ls | tee /dev/fd/3 | ./script.sh ; } 3>&1 #(3 is an semi-arbirtrary choice of fd)