将多个命令传递到一个命令中 [英] Pipe multiple commands into a single command
本文介绍了将多个命令传递到一个命令中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将多个命令的标准输出传递给单个命令?
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:
Example 2:
(setopt; unsetopt; set) | sort
这篇关于将多个命令传递到一个命令中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文