关于管道在 Bash 中的工作原理的简单解释是什么? [英] What is a simple explanation for how pipes work in Bash?

查看:30
本文介绍了关于管道在 Bash 中的工作原理的简单解释是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常在 Bash 中使用管道,例如:

I often use pipes in Bash, e.g.:

dmesg | less

虽然我知道这输出什么,但它需要 dmesg 并让我用 less 滚动它,我不明白 |是在做.是否只是 > 的反面?

Although I know what this outputs, it takes dmesg and lets me scroll through it with less, I do not understand what the | is doing. Is it simply the opposite of >?

  • 对于 | 的作用,是否有一个简单的或隐喻的解释?
  • 在一条线上使用多个管道时会发生什么?
  • 管道在 Bash 脚本中出现的任何地方的行为是否一致?
  • Is there a simple, or metaphorical explanation for what | does?
  • What goes on when several pipes are used in a single line?
  • Is the behavior of pipes consistent everywhere it appears in a Bash script?

推荐答案

Unix 管道将第一个进程的 STDOUT(标准输出)文件描述符连接到第二个进程的 STDIN(标准输入).然后发生的情况是,当第一个进程写入其 STDOUT 时,第二个进程可以立即(从 STDIN)读取该输出.

A Unix pipe connects the STDOUT (standard output) file descriptor of the first process to the STDIN (standard input) of the second. What happens then is that when the first process writes to its STDOUT, that output can be immediately read (from STDIN) by the second process.

使用多个管道与使用单个管道没有什么不同.每个管道都是独立的,只是简单的链接了相邻进程的STDOUT和STDIN.

Using multiple pipes is no different than using a single pipe. Each pipe is independent, and simply links the STDOUT and STDIN of the adjacent processes.

你的第三个问题有点模棱两可.是的,管道本身在 bash 脚本中的任何地方都是一致的.但是,管道字符 | 可以代表不同的东西.双竖线 (||),例如表示或"运算符.

Your third question is a little bit ambiguous. Yes, pipes, as such, are consistent everywhere in a bash script. However, the pipe character | can represent different things. Double pipe (||), represents the "or" operator, for example.

这篇关于关于管道在 Bash 中的工作原理的简单解释是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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