gpt4 book ai didi

linux - 管道组件在shell中的执行顺序

转载 作者:太空狗 更新时间:2023-10-29 12:01:38 24 4
gpt4 key购买 nike

ls | wc

lswc 在这里以什么顺序执行?或者,|wc 只是 ls 的参数吗?

最佳答案

在表达式 ls | wc,您的 shell 将大致执行以下操作:

  1. 启动两个子 shell A 和 B,A 的标准输出连接到 B 的标准输入。
  2. 在子 shell A 中,启动命令 ls
  3. 在子 shell B 中,启动命令 wc
  4. 等到所有子shell终止
  5. 设置$?为子shell B的退出状态(即wc的退出状态)

bash 联机帮助页有更多详细信息:

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 ... ]

The standard output of command is connected via a pipe to the standard input of command2. This connection is performed before any redirections specified by the command (see REDIRECTION below). If |& is used, the standard error of command is connected to command2's standard input through the pipe; it is shorthand for 2>&1 |. This implicit redirection of the standard error is performed after any redirections specified by the command.

The return status of a pipeline is the exit status of the last command, unless the pipefail option is enabled. If pipefail is enabled, the pipeline's return status is the value of the last (rightmost) command to exit with a non-zero status, or zero if all commands exit successfully. If the reserved word ! precedes a pipeline, the exit status of that pipeline is the logical negation of the exit status as described above. The shell waits for all commands in the pipeline to terminate before returning a value.

If the time reserved word precedes a pipeline, the elapsed as well as user and system time consumed by its execution are reported when the pipeline terminates. The -p option changes the output format to that specified by POSIX. The TIMEFORMAT variable may be set to a format string that specifies how the timing information should be displayed; see the description of TIMEFORMAT under Shell Variables below.

Each command in a pipeline is executed as a separate process (i.e., in a subshell).

关于linux - 管道组件在shell中的执行顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33462120/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com