26

我想弄清楚什么时候做管道 | 或重定向 < > 在命令中优先?

这是我的想法,但需要确认它是如何工作的。

示例 1:

sort < names | head
The pipe runs first:  names|head   then it sorts what is returned from names|head

示例 2:

ls | sort > out.txt
This one seems straight forward by testing, ls|sort then redirects to out.txt

示例 3:

Fill in the blank?  Can you have both a < and a > with a | ???
4

5 回答 5

28

在句法分组方面,>具有<较高的优先级;也就是说,这两个命令是等价的:

sort < names | head
( sort < names ) | head

就像这两个:

ls | sort > out.txt
ls | ( sort > out.txt )

但就顺序而言,|是先执行的;所以,这个命令:

cat in.txt > out1.txt | cat > out2.txt

将填充out1.txt,而不是out2.txt,因为> out1.txt是在之后执行的|,因此会取代它(因此没有输出通过管道输出到cat > out2.txt)。

同样,这个命令:

cat < in1.txt | cat < in2.txt

将打印in2.txt, 不是in1.txt, 因为在< in2.txt之后执行,因此|取代(所以没有输入从 管道输入cat < in1.txt).

于 2012-10-17T19:45:40.193 回答
13

来自man bash(与其他引文一样):

SHELL GRAMMAR
   Simple Commands
       A simple command is a sequence of optional variable assignments followed by
       blank-separated words and redirections, and terminated  by  a  control
       operator. The first word specifies the command to be executed, and is
       passed as argument zero.  The remaining words are passed as arguments
       to the invoked command.

       The return value of a simple command is its exit status, or 128+n if
       the command is terminated by signal n.

   Pipelines
       A pipeline is a sequence of one or more commands separated by one of
       the control operators | or |&.  The format for a pipeline is:

              [time [-p]] [ ! ] command [ [|⎪|&] command2 ... ]

换句话说,一个(简单)命令可以有任意数量的重定向;您也可以将其用作管道的一部分。或者,换一种说法,重定向比管道绑定得更紧密。

有几种方法可以解决这个问题(尽管它们很少是必要的或美观的):

1.您可以制作一个“复合命令”并重定向到它:

 Compound Commands
   A compound command is one of the following:

   (list)  list is executed in a subshell environment (see
           COMMAND EXECUTION ENVIRONMENT below).  Variable
           assignments  and  builtin  commands  that  affect  the
           shell's environment do not remain in effect after the
           command completes.  The return status is the exit status of list.

   { list; }
          list  is  simply  executed  in the current shell environment.  list
          must be terminated with a newline or semicolon.  This is known as a
          group command. The return status is the exit status of list.  Note
          that unlike the metacharacters ( and ), { and } are reserved words
          and must occur where a reserved word is permitted to be recognized.
          Since they do not cause a word break, they must be separated from
          list by whitespace or another shell metacharacter.

所以:

$ echo foo > input
$ { cat | sed 's/^/I saw a line: /'; } < input
I saw a line: foo

2.您可以使用“进程替换”重定向到管道:

Process Substitution
   Process  substitution  is  supported on systems that support named pipes
   (FIFOs) or the /dev/fd method of naming open files.  It takes the form of
   <(list) or >(list).  The process list is run with its input or output
   connected to a FIFO or some file in /dev/fd.  The name of this file is
   passed as  an  argument  to  the  current  command  as the result of the
   expansion.  If the >(list) form is used, writing to the file will provide
   input for list.  If the <(list) form is used, the file passed as an argument
   should be read to obtain the output of list.

所以:

 rici@...$ cat > >(sed 's/^/I saw a line: /') < <(echo foo; echo bar)
 I saw a line: foo
 rici@...$ I saw a line: bar

(为什么提示在输出终止之前出现,以及如何处理它留作练习)。

于 2012-10-17T20:07:48.237 回答
6

这几乎是我在阅读后所理解的(包括ruakh的回答)

首先,如果多次重定向,所有的重定向都会执行,但是只有最后一次的重定向才会生效(假设前面的重定向都没有出错)

  • egcat < in1.txt < in2.txt等价于cat < in2.txt, 除非in1.txt在这种情况下不存在此命令将失败(因为< in1.txt首先执行)

  • 类似地, with cat in.txt > out1.txt > out2.txt, onlyout2.txt将包含 的内容out2.txt,但因为> out1.txt首先执行,out1.txt如果它不存在则将被创建。

管道所做的是将stdout上一个命令的连接到stdin下一个命令的连接,并且该连接位于任何其他重定向之前(来自Bash 手册)。

所以你可以想到

cat in1.txt > out1.txt | cat > out2.txt

作为

cat in1.txt > pipe > out1.txt; cat < pipe > out2.txt

并且应用前面提到的多重重定向规则,我们可以将其简化为

cat in1.txt > out1.txt; cat < pipe > out2.txt

结果: 的内容in1.txt被复制到out1.txt,因为没有写入任何内容pipe


使用 [ruak][3] 的另一个例子,
cat < in1.txt | cat < in2.txt

大致相当于

cat > pipe < in1.txt; cat < pipe < in2.txt

这实际上是

cat > pipe < in1.txt; cat < in2.txt

结果:这一次向 写入了一些东西pipe,但是由于第二次cat读取的in2.txt不是pipe,所以只有 的内容in2.txt被打印出来。如果 位于pipe同一侧(><)重定向的中间,它将被引入。

于 2012-10-18T03:34:13.410 回答
3

将数据放在您喜欢的任何地方有点不正统,但完全合法,<所以我更喜欢这个,因为它更好地说明了从左到右的数据流:

<input.txt sort | head >output.txt

唯一不能做到这一点的是内置控制结构命令(for, if, while)。

# Unfortunately, NOT LEGAL
<input.txt  while read line; do ...; done

请注意,所有这些都是等效的命令,但为避免混淆,您应该只使用第一个或最后一个:

<input.txt grep -l foobar
grep <input.txt -l foobar
grep -l <input.txt foobar
grep -l foobar <input.txt

因为文件名必须总是紧跟在重定向操作符之后,所以我更喜欢省略<文件名和文件名之间的可选空格。

于 2012-10-20T20:56:47.333 回答
1

更正:

示例 1:

sort < names | head

在这种情况下,输入重定向首先运行(名称已排序),然后将其结果通过管道传送到 head。

一般来说,您可以从左到右阅读。标准成语的工作方式如下:

  • 使用输入重定向“<”告诉程序从文件而不是标准输入中读取
  • 使用输出重定向“>”告诉程序输出到文件而不是标准输出
  • 使用管道“program_a | program_b”将通常由 program_a 输出的所有内容带到 stdout,并将其全部直接提供给 program_b,就好像它是从 stdin 读取的一样。
于 2012-10-17T19:42:06.293 回答