[英]How to separate outputs of one-line multiple commands?
I have installed some GNU packages on my macOS Sierra, which include bash
, coreutils
, which
, etc. Now I can use which -a bash | xargs -I % echo % "--version" | sh
我已经在macOS Sierra上安装了一些GNU软件包,其中包括bash
, coreutils
, which
等。现在,我可以使用which -a bash | xargs -I % echo % "--version" | sh
which -a bash | xargs -I % echo % "--version" | sh
which -a bash | xargs -I % echo % "--version" | sh
to check all version info of bash
, but there is no separation between two version info: which -a bash | xargs -I % echo % "--version" | sh
检查bash
所有版本信息,但是两个版本信息之间没有分隔:
$ which -a bash | xargs -I % echo % "--version" | sh
GNU bash, version 4.4.12(1)-release (x86_64-apple-darwin16.3.0)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
# There should be one or more blank lines as separation.
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin16)
Copyright (C) 2007 Free Software Foundation, Inc.
I tried ... echo -e % "--version\\n" ...
, but it can't work. 我试过了... echo -e % "--version\\n" ...
,但是它不起作用。 Please help me, thanks. 请帮助我,谢谢。
For a bit more control of the output, use a loop: 要对输出进行更多控制,请使用循环:
which -a bash |
while read cmd; do
printf '%s\t----------\n' "$cmd"
command "$cmd" --version
echo
done
I'd write this as follows: 我会这样写:
IFS=: read -r -a path_entries <<<"$PATH"
find "${path_entries[@]}" -maxdepth 1 -name bash -type f -exec '{}' --version ';'
Note: 注意:
which
. 没有用的which
。 This isn't a shell builtin in bash, so it gives you none of the benefits you'd get from type
(ie. awareness of functions and aliases); 这不是bash内置的shell,因此它不会给您任何type
带来的好处(即,对功能和别名的了解)。 and because its output is line-oriented, it can't unambiguously emit all possible filenames (such as those containing newlines -- which, yes, are valid). 并且由于其输出是面向行的,因此它不能明确地发出所有可能的文件名(例如包含换行符的文件名,是的,这是有效的)。 {}
) is passed as a single token, find -exec
substitutes the exact filename returned for that token. 当占位符( {}
)作为单个令牌传递时, find -exec
替换该令牌返回的确切文件名。 Because the domain of possible argv values (all valid C strings, which is to say, no strings containing NULs) matches the domain of possible filenames on common systems, this avoids introducing potential bugs. 由于可能的argv值的域(所有有效的C字符串,也就是说,没有包含NUL的字符串)与常见系统上可能的文件名的域匹配,因此避免了引入潜在的错误。 /tmp/$(rm -rf ~)
, and that directory contained a bash
executable. 这是一种极易出错的技术,会引起外壳注入漏洞:考虑一下PATH包含/tmp/$(rm -rf ~)
且该目录包含bash
可执行文件的情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.