Linux Bash:检查stdin,但将它完整地转发给另一个程序?
这里有许多例子,bash在读取行时使用Linux Bash:检查stdin,但将它完整地转发给另一个程序?,linux,bash,stdin,Linux,Bash,Stdin,这里有许多例子,bash在读取行时使用,以接收stdin 但是,我只想检查stdin,而不是销毁或修改它,并在退出时将其完整地转发给另一个程序(预期stdin) 可能吗?这是tee解决方案吗?没有tee,是否可以完成此操作 注意:在这种情况下,stdin可能相当大,并且/或者包含二进制,因此我不想将其读入字符串,我只需要检查它的开头。当您必须在检查输出后确定如何启动第二个程序时,您必须等待第一个程序完成。因此,您可以让考官使用存储在文件中的输入启动secondprog firstprog | t
,以接收stdin
但是,我只想检查stdin
,而不是销毁或修改它,并在退出时将其完整地转发给另一个程序(预期stdin)
可能吗?这是tee
解决方案吗?没有tee
,是否可以完成此操作
注意:在这种情况下,stdin
可能相当大,并且/或者包含二进制,因此我不想将其读入字符串,我只需要检查它的开头。当您必须在检查输出后确定如何启动第二个程序时,您必须等待第一个程序完成。因此,您可以让考官使用存储在文件中的输入启动secondprog
firstprog | tee outfile | myfilter.sh
使用一些逻辑来创建选项列表(创建类似my_inspect的函数)
optionlist=“”
而read-r行;做
optionlist+=$(我的检查“${line}”)
完成
secondprog${optionlist}
您可以为此使用coproc
和group命令({}
)。我得出了以下结论:
coproc cat .profile # our "firstprog"
exec 200<&${COPROC[0]} # to keep it open after the first read
while read -r line; do
firstline="$line"
break
done <&200 # feed the loop from our new filedescriptor
{ # open group command to batch the output of the embedded commands
echo FIRST LINE WAS: $firstline # reprint the read line(s)
cat <&200 # copy the rest...
} | sed 's:^:_ :g' # our "secondprog" just to see things modified
coproc cat.profile#我们的“第一个程序”
exec 200您需要对输入执行什么操作?您的脚本需要根据它执行操作,还是您只想自己查看它?如果您需要实时查看数据,那么我认为您唯一的选择是读取所需的数据,然后重新输出所有其他内容。听起来像是tee
和进程替换的工作(这标记得很好!):类似于firstprog | while read-r line;检查“${line}”;回显“${line}”;完成| secondprog
从流中读取数据会从该流中删除数据。如果希望输入数据由两个不同的进程使用,则必须将其中一个进程转发给另一个进程,或者第三个程序(如tee
)必须拆分流,将其转发给两个进程。但是,请注意,tee
的一个输出总是指向一个文件,因此您必须更努力地将其转换为进程的输入。@WalterA另一个问题是,我不能直接通过管道传输到第二个prog,因为我可能必须根据我正在检查的stdin修改其命令行参数-复杂,我知道!
coproc cat .profile # our "firstprog"
exec 200<&${COPROC[0]} # to keep it open after the first read
while read -r line; do
firstline="$line"
break
done <&200 # feed the loop from our new filedescriptor
{ # open group command to batch the output of the embedded commands
echo FIRST LINE WAS: $firstline # reprint the read line(s)
cat <&200 # copy the rest...
} | sed 's:^:_ :g' # our "secondprog" just to see things modified