简体   繁体   English

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

[英]How can bash be used to pipe stdout to a script AND write to terminal?

I would like to pipe the output of a job to a script to read in that stdout lines and complete actions and display the output on the terminal. 我想将作业的输出传递到脚本以读取标准输出行并完成操作并在终端上显示输出。

Right now, I have this.. 现在,我有这个。

ls | ./script.sh

This allows my script to be run on the output, but does not display the result of ls on the terminal. 这允许我的脚本在输出上运行,但不会在终端上显示ls的结果。

I have tried this: 我已经试过了:

ls | tee ./script.sh

but this overwrites the contents of script.sh with the output from ls. 但这会用ls的输出覆盖 script.sh 的内容

How can I show the output of "ls" on my terminal, and run the contents on script.sh over that input? 如何在终端上显示“ ls”的输出,并在该输入上运行script.sh上的内容? Here is an example of what my script.sh looks like: 这是我的script.sh的示例:

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

You can do: 你可以做:

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

or, if you want to use exactly what stdout was before the piping, you can do something like: 或者,如果您想使用管道之前的标准输出,可以执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM