我如何运行包含shell命令和管道的变量?

问题描述 投票:1回答:2

Bash中,我需要运行包含commandsvariable,然后将输出分配给另一个变量。问题是有几个命令,还有一些管道或类似的东西。

下面是一个示例:

snmpwalk -Ov -v 2c -c public 127.0.0.1 1.3.6.1.4.1.6574.1.2.0 | awk "{print $ 2}"

和:

upsc ups | grep input.voltage: | cut -d" " -f2

我该怎么做?

linux bash shell eval
2个回答
1
投票

您可以将任何命令或管道的标准输出捕获到这样的变量:

result=$(upsc ups | grep input.voltage: | cut -d" " -f2)

0
投票

这是一种方式:

cmd='upsc ups | grep input.voltage: | cut -d" " -f2'
result=`echo "$cmd" | bash`
© www.soinside.com 2019 - 2024. All rights reserved.