Serge S
Serge S

Reputation: 73

How can I run a variable containing shell commands and pipes?

In Bash I need to run a variable containing commands, and then assign the output to another variable. The problem is there are several commands and there are some pipes or something like that.

Below is a sample:

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

And:

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

How can I do this?

Upvotes: 1

Views: 77

Answers (2)

webb
webb

Reputation: 4340

Here's a way:

cmd='upsc ups | grep input.voltage: | cut -d" " -f2'
result=`echo "$cmd" | bash`

Upvotes: 1

John Zwinck
John Zwinck

Reputation: 249592

You can capture the stdout of any command or pipeline to a variable like this:

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

Upvotes: 0

Related Questions