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

雨燕双飞 提交于 2019-12-04 04:19:37

问题


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 regex so please don't redirect me to that.

This is my code:

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

I want to be able to run something like

sh kill_proc.sh node and have it run

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?


回答1:


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

or

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

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 $2}'

the shell expands it to something like this:

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

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

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.

(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.)

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).




回答2:


You need to use echo (or printf) to actually put the value of $LINE onto the standard input of the awk command.

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

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

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

or better, skip the grep:

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

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.



来源:https://stackoverflow.com/questions/25535642/how-do-i-store-the-output-of-a-bash-command-in-a-variable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!