如何将 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
How do i store the output of a bash command in a variable?
提问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 node
and 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 $PROCESS
it 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 $LINE
to make the example readable.)
(我缩短了 的值$LINE
以使示例可读。)
The first subcommand of the pipeline sets variable PROCESS
; this variable-setting command has no output so awk
reads EOF immediately and prints nothing. And since each subcommand of the pipeline runs in a subshell, the setting of PROCESS
takes place only in a subshell, not in the parent shell running the script, so PROCESS
is still not set for later commands in your script.
管道的第一个子命令设置变量PROCESS
;这个变量设置命令没有输出,所以awk
立即读取 EOF 并且不打印任何内容。并且由于管道的每个子命令都在子 shell 中运行,因此设置PROCESS
仅在子 shell 中进行,而不是在运行脚本的父 shell 中进行,因此PROCESS
仍然没有为脚本中的后续命令设置。
(Note that some versions of bash
can 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 PROCESS
in a subshell and feeding nothing to awk
on standard input, you want to feed the value of LINE
to awk
and store the result in PROCESS
in the current shell. So you need to run a command that writes the value of LINE
to its standard output, and connects that standard output to the standard input of awk
. The echo
command can do this (or the printf
command, as chepner pointed out in his answer).
相反,设定PROCESS
在一个子shell和喂养没什么awk
标准输入,你要养活的价值LINE
来awk
,结果存储在PROCESS
当前外壳。因此,您需要运行一个命令,将 的值写入LINE
其标准输出,并将该标准输出连接到 的标准输入awk
。该echo
命令可以做到这一点(或printf
命令,如chepner在他的回答中指出)。
回答by chepner
You need to use echo
(or printf
) to actually put the value of $LINE
onto the standard input of the awk
command.
您需要使用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 PROCESS
with 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"
应该足够了。