Wednesday, 15 April 2015

Variable within a Variable in Bash -



Variable within a Variable in Bash -

can not figure out. can't figure out variable within variable. also, how quiet output if host down.

#/bin/bash ip in `cat list`; output=$( $name=`snmpwalk -v 2c -c snmp2 $ip snmpv2-mib::sysname.0 | grep -o '[^ ]*$'` $type=`snmpwalk -v 2c -c snmp2 $ip snmpv2-smi::mib-2.47.1.1.1.1.13.1 | grep -o '[^ ]*$'` $soft=`snmpwalk -v 2c -c snmp2 $ip 1.3.6.1.2.1.1.1.0 | grep -i ios | awk -f, '{print $2,$3}'` ) if [ $? -eq 0 ]; echo $ip,echo $output[$name][$type][$soft] else echo $ip,null fi done

$name=value wrong. in bash it's name=value.

the $name etc. variables declared in subshell, won't available in outside code.

some other issues code:

it checks exit code of last snmpwalk. utilize set -o errexit own sanity. use $(foo) command substitutions. `foo` obsolete. use more quotes! useless utilize of cat award awarded. use portable shebang line (disclaimer: own answer) echo foo,echo bar print foo,echo bar. ; line separator, whenever sense tempted utilize add together newline clarity.

bash

No comments:

Post a Comment