复制标准输入到标准输出 [英] Duplicate stdin to stdout

查看:147
本文介绍了复制标准输入到标准输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要寻找一个bash一行程序的标准输入复制到标准输出,而不交错。到目前为止,我发现的唯一的解决办法是使用 T恤,但这并产生交错式输出。我是什么意思:

如果如文件˚F

  A
b

我想执行

 猫F | HERE_BE_COMMAND

获得

  A
b
一个
b

如果我用 T恤 - 为命令,输出通常看起来像

  A
一个
b
b

一个干净的解决方案有什么建议?

澄清

猫˚F<​​/ code>命令只是其中输入可以来自一个例子。在现实中,它是一个可以(应)只被执行一次的命令。我也想使用临时文件不要,因为处理的数据的排序的时候所执行的命令被中断的敏感文件和临时文件总是容易出错。此外,我没有兴趣在涉及其他脚本(如在上文所述,它应该是一个的单行的),或者需要进行实际的复制命令之前执行preparatory命令


解决方案

解决方案1:

 &LT; command_which_produces_output&GT; | {一个=$(小于的/ dev /标准输入);回声$ A;回声$ A; }

在这种方式,你保存从标准输入的内容 A (选择一个更好的名字吗),然后回声ING的两倍。

通知 $(小于为/ dev /标准输入)是做 $(猫的/ dev /标准输入)类似,但更有效的方式


解决方案2:

使用通过以下方式三通

 &LT; command_which_produces_output&GT; |三通&GT;(回声$(小于为/ dev /标准输入))

在这里,你会首先写入标准输出(这就是 T恤一样),也写入通过的进程替换

 &GT;(回声$(小于为/ dev /标准输入))

请参阅例如在我的系统中它创建的文件:

  $回声&GT;(回声$(小于为/ dev /标准输入))
为/ dev / FD / 63

现在,在回声$(小于为/ dev /标准输入)部分只是我发现在打印前先阅读整个文件的方式。它回声ES从进程替换的标准输入读取内容,但一旦所有的输入被读取(而不是像通过行打印线)。

I am looking for a bash one-liner that duplicates stdin to stdout without interleaving. The only solution I have found so far is to use tee, but that does produced interleaved output. What do I mean by this:

If e.g. a file f reads

a
b

I would like to execute

cat f | HERE_BE_COMMAND

to obtain

a
b
a
b

If I use tee - as the command, the output typically looks something like

a
a
b
b

Any suggestions for a clean solution?

Clarification

The cat f command is just an example of where the input can come from. In reality, it is a command that can (should) only be executed once. I also want to refrain from using temporary files, as the processed data is sort of sensitive and temporary files are always error-prone when the executed command gets interrupted. Furthermore, I am not interested in a solution that involves additional scripts (as stated above, it should be a one-liner) or preparatory commands that need to be executed prior to the actual duplication command.

解决方案

Solution 1:

<command_which_produces_output> | { a="$(</dev/stdin)";  echo "$a"; echo "$a"; }

In this way, you're saving the content from the standard input in a (choose a better name please), and then echo'ing twice.

Notice $(</dev/stdin) is a similar but more efficient way to do $(cat /dev/stdin).


Solution 2:

Use tee in the following way:

<command_which_produces_output> | tee >(echo "$(</dev/stdin)")

Here, you're firstly writing to the standard output (that's what tee does), and also writing to a FIFO file created by process substitution:

>(echo "$(</dev/stdin)")

See for example the file it creates in my system:

$ echo >(echo "$(</dev/stdin)")
/dev/fd/63

Now, the echo "$(</dev/stdin)" part is just the way I found to firstly read the entire file before printing it. It echo'es the content read from the process substitution's standard input, but once all the input is read (not like cat that prints line by line).

这篇关于复制标准输入到标准输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆