bash:如何连接两个命令的输出,以便我可以将它们通过管道传输到第三个?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7499013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
bash: how do I concatenate the output of two commands so that I can pipe them to a third?
提问by John Lawrence Aspden
$ hg status
and
和
$ hg status --ignored
give very similar outputs. I'd like to concatenate them so I can feed them to awk, as if there were an hg status --all(or svn's svn status --no-ignore)
给出非常相似的输出。我想将它们连接起来,这样我就可以将它们提供给 awk,就好像有一个hg status --all(或 svn 的svn status --no-ignore)
I'm thinking something like:
我在想这样的事情:
$ echo "$(hg status)" "$(hg status --ignored)" | awk ' ( == "?" ) || ( == "I") { print }' | xargs rm -r
to make a 'make very clean indeed' command, but it seems to occasionally leave a file behind, perhaps because a newline goes missing or something.
制作一个“确实非常干净”的命令,但它似乎偶尔会留下一个文件,可能是因为缺少换行符或其他原因。
回答by Alan Haggai Alavi
You can use a subshell:
您可以使用子shell:
( hg status; hg status --ignored ) | awk '( == "?" ) || ( == "I") { print }' | xargs rm -r
回答by Vincent Scheib
Use curly braces to group commands:
使用花括号对命令进行分组:
$ { echo first line; echo second line; } | grep "line"
first line
second line
(Posted as an answer from camh's comment)
(作为来自camh评论的答案发布)
回答by richq
You can use the rest of the hg status flags to show what you really want:
您可以使用其余的 hg 状态标志来显示您真正想要的内容:
hg status -uriamn
That shows unknown files (u), removed files (r), ignored (i), added (a), modified (m) and does so without showing the status prefix.
显示未知文件 (u)、删除文件 (r)、忽略 (i)、添加 (a)、修改 (m) 并且不显示状态前缀。
回答by animtakhnet
This works for me:
这对我有用:
echo $(a)$(b)
if you add "" you can add delimiters eg.:
如果添加“”,则可以添加分隔符,例如:
echo "$(./gethostname.sh)|($(./getip.sh);"
I use this on Openwrt to broadcast my ip settings:
我在 Openwrt 上使用它来广播我的 ip 设置:
echo "$( uci get system.@system[0].hostname )|$( ip addr | grep inet | grep br-lan | cut -d ' ' -f 6 | cut -d '/' -f 1 );" | socat - UDP-DATAGRAM:255.255.255.255:4999,broadcast ;

