将多个命令通过管道传输到一个命令中 [英] Pipe multiple commands into a single command

查看:28
本文介绍了将多个命令通过管道传输到一个命令中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将多个命令的标准输出通过管道传输到单个命令?

How can I pipe the stdout of multiple commands to a single command?

示例 1:对所有三个 echo 命令的输出进行组合和排序:

Example 1: combine and sort the output of all three echo commands:

echo zzz; echo aaa; echo kkk

期望的输出:

aaa
kkk
zzz

示例 2:重写以下内容,以便所有命令都在使用管道的单个命令行中,而不重定向到临时文件:

Example 2: rewrite the following so that all the commands are in a single command-line using pipes, without redirects to a temp file:

setopt > /tmp/foo; unsetopt >> /tmp/foo; set >> /tmp/foo; sort /tmp/foo

推荐答案

使用括号 () 将命令组合成一个进程,这将连接每个进程的标准输出.

Use parentheses ()'s to combine the commands into a single process, which will concatenate the stdout of each of them.

示例1(注意$是shell提示符):

Example 1 (note that $ is the shell prompt):

$ (echo zzz; echo aaa; echo kkk) | sort
aaa
kkk
zzz


示例 2:

(setopt; unsetopt; set) | sort

这篇关于将多个命令通过管道传输到一个命令中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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