在Linux下如何管工作? [英] How Does Piping Work in Linux?

查看:175
本文介绍了在Linux下如何管工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能是一个简单的问题,但...

Probably a simple question but...

如何管工作?如果我运行通过CLI程序并重定向输出到一个文件,我将能够在该文件到另一个程序管,因为它正在写的?

How does piping work? If I run a program via CLI and redirect output to a file will I be able to pipe that file into another program as it is being written?

基本上当一行被写入文件,我想它要立即输送到我的第二个应用程序(我试图动态绘制图形关闭现有的程序)。如果管道完成移动到下一个命令之前第一个命令只是不确定。

Basically when one line is written to the file I would like it to be piped immediately to my second application (I am trying to dynamically draw a graph off an existing program). Just unsure if piping completes the first command before moving on to the next command.

任何反馈会大大AP preciated!

Any feed back would be greatly appreciated!

推荐答案

如果你想一个程序的输出重定向到另一个的输入,只需使用一个简单的管道:

If you want to redirect the output of one program into the input of another, just use a simple pipeline:

program1 arg arg | program2 arg arg

如果你想程序1 的输出保存到一个文件中的的管道将它导入 Program2中,您可以使用 T恤(1)

If you want to save the output of program1 into a file and pipe it into program2, you can use tee(1):

program1 arg arg | tee output-file | program2 arg arg

在管道中的所有程序同时运行。大多数程序通常使用的拦截的I / O:如果当他们尝试读取输入和无所不有,他们的的:那就是,他们停下来,和操作系统德-schedules它们运行到更多的输入可用(为了避免吃起来CPU)。同样,如果先前在管道中的程序写入数据比后面的程序更快的可以读取它,最终管道缓冲区填满和作家块:操作系统去的时间表,直到管道缓冲区得到由读者清空,然后它可以继续写再次

All programs in a pipeline are run simultaneously. Most programs typically use blocking I/O: if when they try to read their input and nothing is there, they block: that is, they stop, and the operating system de-schedules them to run until more input becomes available (to avoid eating up the CPU). Similarly, if a program earlier in the pipeline is writing data faster than a later program can read it, eventually the pipe's buffer fills up and the writer blocks: the OS de-schedules it until the pipe's buffer gets emptied by the reader, and then it can continue writing again.


修改

如果你想使用的程序1 作为命令行参数,你可以使用反引号或 $()语法:

If you want to use the output of program1 as the command-line parameters, you can use the backquotes or the $() syntax:

# Runs "program1 arg", and uses the output as the command-line arguments for
# program2
program2 `program1 arg`

# Same as above
program2 $(program1 arg)

$()语法应为preferred,因为它们是更清晰,而且可以嵌套。

The $() syntax should be preferred, since they are clearer, and they can be nested.

这篇关于在Linux下如何管工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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