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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:43:49  来源:igfitidea点击:

Piping output to cut

linuxbashshell

提问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 $PPIDincludes 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 echooutputs all of its arguments separated by spaces). To address this, you can use trto "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 pson its own, you'll see why your pipe doesn't work:

我想如果你echo ps自己运行,你会明白为什么你的管道不起作用:

$ echo ps
ps

Instead, check $0. Note that it might be -bashor 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. 请注意,它可能是-bashbash,具体取决于它是否是登录 shell。(好吧,任何外壳——不仅仅是bash——但如果外壳是登录外壳,则会添加连字符。)

回答by icyrock.com

The first line:

第一行:

echo $(ps | grep $PPID) | cut -d" " -f4

says:

说:

  • Execute ps | grep $PPIDin 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" " -f4on 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 $PPIDcontains a number, so it will never be ps. Thus, grep returns nothing
  • Execute cut -d" " -f4with 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