Linux 管道输出切割
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9388623/
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
Piping output to cut
提问by ZPS
I am trying to get the name of the shell executing a script.
我正在尝试获取执行脚本的 shell 的名称。
Why does
为什么
echo $(ps | grep $PPID) | cut -d" " -f4
work while
工作的同时
echo ps | grep $PPID | cut -d" " -f4
does not?
才不是?
采纳答案by ruakh
The reason is that
原因是
echo ps
just prints out the string ps
; it doesn't run the program ps
. The corrected version of your command would be:
只是打印出字符串ps
;它不运行程序ps
。您的命令的更正版本将是:
ps | grep $PPID | cut -d" " -f4
Edited to add:paxdiablo points out that ps | grep $PPID
includes a lot of whitespace that will get collapsed by echo $(ps | grep $PPID)
(since the result of $(...)
, when it's not in double-quotes, is split by whitespace into separate arguments, and then echo
outputs all of its arguments separated by spaces). To address this, you can use tr
to "squeeze" repeated spaces:
编辑添加:paxdiablo 指出ps | grep $PPID
包含大量空格,这些空格将被折叠echo $(ps | grep $PPID)
(因为 的结果$(...)
,当它不在双引号中时,被空格分割成单独的参数,然后echo
输出所有由空格分隔的参数)。为了解决这个问题,您可以使用tr
“挤压”重复空格:
ps | grep $PPID | tr -s ' ' | cut -d' ' -f5
or you can just stick with what you had to begin with. :-)
或者你可以坚持你必须开始的东西。:-)
回答by sarnold
I think if you run just echo ps
on its own, you'll see why your pipe doesn't work:
我想如果你echo ps
自己运行,你会明白为什么你的管道不起作用:
$ echo ps
ps
Instead, check $0
. Note that it might be -bash
or bash
, depending if it is a login shell. (Well, any shell -- not just bash
-- but the hyphenis added if the shell is a login shell.)
相反,检查$0
. 请注意,它可能是-bash
或bash
,具体取决于它是否是登录 shell。(好吧,任何外壳——不仅仅是bash
——但如果外壳是登录外壳,则会添加连字符。)
回答by icyrock.com
The first line:
第一行:
echo $(ps | grep $PPID) | cut -d" " -f4
says:
说:
- Execute
ps | grep $PPID
in a sub-shell Return the output - which will be something like this:
3559 pts/1 00:00:00 bash
and then use that output as the first parameter of echo - which, in practice, means just echo the output
- Then run
cut -d" " -f4
on that - which gives you the command name in this case
ps | grep $PPID
在子shell中执行返回输出 - 这将是这样的:
3559 pts/1 00:00:00 bash
然后将该输出用作 echo 的第一个参数 - 实际上,这意味着只回显输出
- 然后运行
cut -d" " -f4
它 - 在这种情况下它会为您提供命令名称
The second command:
第二条命令:
echo ps | grep $PPID | cut -d" " -f4
says:
说:
- Echo string
ps
- Grep that string for
$PPID
- this will never return anything, as$PPID
contains a number, so it will never beps
. Thus, grep returns nothing - Execute
cut -d" " -f4
with input of the previous command - which is nothing, so you get nothing
- 回声串
ps
- Grep 该字符串 for
$PPID
- 这将永远不会返回任何内容,因为$PPID
包含一个数字,所以它永远不会是ps
. 因此,grep 不返回任何内容 - 执行
cut -d" " -f4
与前一个命令的输入-这是什么,所以你什么也得不到
回答by CognizantApe
For many uses it is better to just us tr
. For example:
对于许多用途,最好只使用我们tr
。例如:
Input:
echo "field1,field2,field3" | tr , \\n
Output:
field1
field2
field3
输入:
echo "field1,field2,field3" | tr , \\n
输出:
field1
field2
field3