John
John

Reputation: 61

bash script selecting variable based on command line parameter

I have a bash script that starts like this:

#!/bin/bash
systemStateGlobalSystemState=.1.3.6.1.4.1.674.10909.1.200.10.1.2
systemStateChassisStatus=.1.3.6.1.4.1.674.10909.1.200.10.1.4
systemStateVoltageStatusCombined=.1.3.6.1.4.1.674.10909.1.200.10.1.12
systemStateTemperatureStatusCombined=.1.3.6.1.4.1.674.10909.1.200.10.1.24
systemStateMemoryDeviceStatusCombined=.1.3.6.1.4.1.674.10909.1.200.10.1.27
         systemStateChassisIntrusionStatusCombined=.1.3.6.1.4.1.674.10909.1.200.10.1.30
operatingSystemMemoryStatus=.1.3.6.1.4.1.674.10909.1.400.20.1.4

RESULT=$(/usr/local/nagios/libexec/check_snmp -H $1 -o $2 -c $3)
CODE=$(echo $RESULT | awk '{print $4}')

What I am trying to do is if someone for $2 command line parameter enters operatingSystemMemoryStatus how do I select the predefined value for it in the script? So where $2 above is referenced in RESULT, how do I get that command to use the .1.3.6.1.4.1.674.10909.1.400.20.1.4 value?

So if I entered

check_snmp 192.168.0.1 operatingSystemMemoryStatus public script  

would do:

/usr/local/nagios/libexec/check_snmp -H 192.168.0.1  -o .1.3.6.1.4.1.674.10909.1.400.20.1.4 -c public

How can I accomplish this?

Upvotes: 1

Views: 429

Answers (3)

Walter A
Walter A

Reputation: 20012

Use a switch construction. I added a 3-letter alternative for all options:

case $2 in
   "gss"|"systemStateGlobalSystemState")
       check=".1.3.6.1.4.1.674.10909.1.200.10.1.2"
       ;;
   "scs"|"systemStateChassisStatus")
       check=".1.3.6.1.4.1.674.10909.1.200.10.1.4"
   ;;
   "vsc"|"systemStateVoltageStatusCombined")
       check=".1.3.6.1.4.1.674.10909.1.200.10.1.12"
   ;;
   "tsc"|"systemStateTemperatureStatusCombined")
       check=".1.3.6.1.4.1.674.10909.1.200.10.1.24"
   ;;
   "dsc"|"systemStateMemoryDeviceStatusCombined")
       check=".1.3.6.1.4.1.674.10909.1.200.10.1.27"
   ;;
   "isc"|"systemStateChassisIntrusionStatusCombined")
       check=".1.3.6.1.4.1.674.10909.1.200.10.1.30"
   ;;
   "sms"|"operatingSystemMemoryStatus")
       check=".1.3.6.1.4.1.674.10909.1.400.20.1.4"
   ;;
   *) echo "Invalid option $1"
   ;;
esac
echo "Use $check now"

I have just copied/pasted the values. Not the best way, you can introduce variables that will make comparing and updating values easier:

precheck=".1.3.6.1.4.1.674.10909.1"
systemcheck="200.10.1"
oscheck="400.20.1"
    case $2 in
       "gss"|"systemStateGlobalSystemState")
           check="${precheck}.${systemcheck}.2"
           ;;
       "scs"|"systemStateChassisStatus")
           check="${precheck}.${systemcheck}.4"
       ;;
       "vsc"|"systemStateVoltageStatusCombined")
           check="${precheck}.${systemcheck}.12"
       ;;
       "tsc"|"systemStateTemperatureStatusCombined")
           check="${precheck}.${systemcheck}.24"
       ;;
       "dsc"|"systemStateMemoryDeviceStatusCombined")
           check="${precheck}.${systemcheck}.27"
       ;;
       "isc"|"systemStateChassisIntrusionStatusCombined")
           check="${precheck}.${systemcheck}.30"
       ;;
       "sms"|"operatingSystemMemoryStatus")
           check="${precheck}.${oscheck}.4"
       ;;
       *) echo "Invalid option $1"
       ;;
    esac
    echo "Use $check now"

Upvotes: 1

l0b0
l0b0

Reputation: 58858

You can use an associative array to achieve this:

declare -A lookup=(['foo']='bar' ['x']='y')
echo "${lookup[foo]}" # Prints "bar"

Upvotes: 1

gilhad
gilhad

Reputation: 609

I think, you want to use indirect expansion like this:

#!/bin/bash
systemStateGlobalSystemState=.1.3.6.1.4.1.674.10909.1.200.10.1.2
systemStateChassisStatus=.1.3.6.1.4.1.674.10909.1.200.10.1.4
systemStateVoltageStatusCombined=.1.3.6.1.4.1.674.10909.1.200.10.1.12
systemStateTemperatureStatusCombined=.1.3.6.1.4.1.674.10909.1.200.10.1.24
systemStateMemoryDeviceStatusCombined=.1.3.6.1.4.1.674.10909.1.200.10.1.27
systemStateChassisIntrusionStatusCombined=.1.3.6.1.4.1.674.10909.1.200.10.1.30
operatingSystemMemoryStatus=.1.3.6.1.4.1.674.10909.1.400.20.1.4

echo "you are looking for ${!2} value"
RESULT=$(/usr/local/nagios/libexec/check_snmp -H $1 -o ${!2} -c $3)
CODE=$(echo $RESULT | awk '{print $4}')

and call it like that

./myscript 192.168.0.1 operatingSystemMemoryStatus public script

you are looking for .1.3.6.1.4.1.674.10909.1.400.20.1.4 value
<expected result, which I cannot simulate on my box>

in man bash you can read

   If the first character of parameter is an exclamation point (!), it introduces a level of variable indirection.  Bash uses the value of the variable  formed
   from  the  rest of parameter as the name of the variable; this variable is then expanded and that value is used in the rest of the substitution, rather than
   the value of parameter itself.  This is known as indirect expansion. 

Upvotes: 1

Related Questions