我如何保存bash命令的一个变量输出? [英] How do i store the output of a bash command in a variable?

查看:154
本文介绍了我如何保存bash命令的一个变量输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着写一个简单的脚本杀害的过程。我已经读过<一个href=\"http://stackoverflow.com/questions/3510673/find-and-kill-a-process-in-one-line-using-bash-and-regex\">Find并使用bash和正则表达式所以请不要我重定向到杀死在一行中的过程。

这是我的code:

  LINE = $(的ps aux | grep的$ 1)
PROCESS = $ LINE | AWK'{打印$ 2}'
回声$ PROCESS
杀-9 $ PROCESS

我希望能够运行像

SH kill_proc.sh节点,并让它运行

杀-9节点

但不是我所得到的是

  kill_process.sh:2号线:用户:命令未找到

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


解决方案

  PROCESS = $(回声$ LINE| awk的'{$打印2})

  PROCESS = $(的ps aux | grep的$ 1| awk的'{$打印2})

我不知道为什么你得到你所引用的错误。我不能复制。当你这样说:

  PROCESS = $ LINE | AWK'{打印$ 2}'

壳它扩展为这样的事情:

  PROCESS ='mayoff 10732 ...| AWK'{打印$ 2}'

(我已经缩短 $行的值,使例子可读性。)

管道的第一个命令设置变量 PROCESS ;这个变量设定命令没有输出,所以 AWK 读取EOF立即打印什么。而且,由于在子shell管道运行的每个子,处理的设置只发生在子shell,而不是在父shell中运行的脚本,因此 PROCESS 还是不能设置为在脚本后命令。

(注意:庆典的某些版本可以运行在当前shell,而不是在子shell管道的最后一个子命令,但这并不影响这个例子。 )

而不是设置 PROCESS 在子shell和标准输入喂养没什么<$​​ C $ C> AWK 的,你要养活的价值 AWK 并把结果存储在 PROCESS 在当前shell。所以,你需要运行一个写入<$​​ C $ C>行到其标准输出的值的命令,并连接该标准输出到 awk的。在回声命令可以做到这一点(或的printf 命令,chepner在他的回答中指出)。

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?

解决方案

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

这篇关于我如何保存bash命令的一个变量输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆