Reputation: 73
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
Reputation: 4340
Here's a way:
cmd='upsc ups | grep input.voltage: | cut -d" " -f2'
result=`echo "$cmd" | bash`
Upvotes: 1
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