如何将 bash 命令的输出存储在变量中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25535642/
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-09-18 11:15:03  来源:igfitidea点击:

How do i store the output of a bash command in a variable?

bashshellsh

提问by dopatraman

I'm trying to write a simple script for killing a process. I've already read Find and kill a process in one line using bash and regexso please don't redirect me to that.

我正在尝试编写一个简单的脚本来终止进程。我已经阅读了使用 bash 和 regex 在一行中查找并终止进程,所以请不要将我重定向到该处。

This is my code:

这是我的代码:

LINE=$(ps aux | grep '')
PROCESS=$LINE | awk '{print }'
echo $PROCESS
kill -9 $PROCESS

I want to be able to run something like

我希望能够运行类似的东西

sh kill_proc.sh nodeand have it run

sh kill_proc.sh node让它运行

kill -9 node

kill -9 node

But instead what I get is

但我得到的是

kill_process.sh: line 2: User: command not found

I found out that when I log $PROCESSit is empty. Does anyone know what I'm doing wrong?

我发现当我登录时$PROCESS它是空的。有谁知道我做错了什么?

采纳答案by rob mayoff

PROCESS=$(echo "$LINE" | awk '{print }')

or

或者

PROCESS=$(ps aux | grep "" | awk '{print }')

I don't know why you're getting the error you quoted. I can't reproduce it. When you say this:

我不知道为什么你会收到你引用的错误。我无法重现它。当你这样说时:

PROCESS=$LINE | awk '{print }'

the shell expands it to something like this:

外壳将其扩展为如下所示:

PROCESS='mayoff  10732 ...' | awk '{print }'

(I've shortened the value of $LINEto make the example readable.)

(我缩短了 的值$LINE以使示例可读。)

The first subcommand of the pipeline sets variable PROCESS; this variable-setting command has no output so awkreads EOF immediately and prints nothing. And since each subcommand of the pipeline runs in a subshell, the setting of PROCESStakes place only in a subshell, not in the parent shell running the script, so PROCESSis still not set for later commands in your script.

管道的第一个子命令设置变量PROCESS;这个变量设置命令没有输出,所以awk立即读取 EOF 并且不打印任何内容。并且由于管道的每个子命令都在子 shell 中运行,因此设置PROCESS仅在子 shell 中进行,而不是在运行脚本的父 shell 中进行,因此PROCESS仍然没有为脚本中的后续命令设置。

(Note that some versions of bashcan run the last subcommand of the pipeline in the current shell instead of in a subshell, but that doesn't affect this example.)

(请注意,某些版本的bash可以在当前 shell 而不是子 shell 中运行管道的最后一个子命令,但这不会影响此示例。)

Instead of setting PROCESSin a subshell and feeding nothing to awkon standard input, you want to feed the value of LINEto awkand store the result in PROCESSin the current shell. So you need to run a command that writes the value of LINEto its standard output, and connects that standard output to the standard input of awk. The echocommand can do this (or the printfcommand, as chepner pointed out in his answer).

相反,设定PROCESS在一个子shell和喂养没什么awk标准输入,你要养活的价值LINEawk,结果存储在PROCESS当前外壳。因此,您需要运行一个命令,将 的值写入LINE其标准输出,并将该标准输出连接到 的标准输入awk。该echo命令可以做到这一点(或printf命令,如chepner在他的回答中指出)。

回答by chepner

You need to use echo(or printf) to actually put the value of $LINEonto the standard input of the awkcommand.

您需要使用echo(或printf) 将 的值实际放到命令$LINE的标准输入上awk

LINE=$(ps aux | grep "")
PROCESS=$(echo "$LINE" | awk '{print }')
echo $PROCESS
kill -9 $PROCESS

There's no need use LINE; you can set PROCESSwith a single line

没必要用LINE;你可以PROCESS用一行设置

PROCESS=$(ps aux | grep "" | awk '{print }')

or better, skip the grep:

或者更好,跳过grep

PROCESS=$(ps aux | awk -v pname="" ' ~ pname {print }')

Finally, don't use kill -9; that's a last resort for debugging faulty programs. For any program that you didn't write yourself, kill "$PROCESS"should be sufficient.

最后,不要使用kill -9; 这是调试错误程序的最后手段。对于任何不是您自己编写的程序,kill "$PROCESS"应该足够了。