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?
eval. But be wary thatevalis evil when string is not sanitized. Ever heard of Exploits of a Mom?awkprogram and the-d" "argument tocut, because they contain spaces which make plain string handling into a complete pain. In principle,eval "${array[@]}"will then 'work', but you have to be so careful it is unbearable (which is why this isn't an answer). Usingarray=(snmpwalk -Ov -v 2c -c public 127.0.0.1 1.3.6.1.4.1.6574.1.2.0 '|' awk "'{print \$2}'")allows you to useeval "${array[$@]}", but note the double quotes around single quotes and the escaped dollar; they don't lend themselves to easy handling. […continued…]