1

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?

tripleee
  • 175,061
  • 34
  • 275
  • 318
John
  • 61
  • 2
  • 7
  • Possible duplicate of [How do I parse command line arguments in bash?](http://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash) – Avihoo Mamka Jan 15 '16 at 19:21
  • Not a duplicate. That post does not show how to take command line params and assign a value based on an existing defined variable of the exact same name. – John Jan 15 '16 at 19:39
  • You could use a associative array like in [this question](http://stackoverflow.com/questions/16553089/bash-dynamic-variable-names). – Corubba Jan 15 '16 at 19:47

3 Answers3

1

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. 
gilhad
  • 609
  • 1
  • 5
  • 22
  • Excellent. That did it. Thanks. Wish I could mark this as the answer to my question. Only lets me mark the response that another thread is my answer. – John Jan 15 '16 at 19:58
  • You should see on top left (before the I think ...." arrow up, zero, arrow down and big V - if you click the V, it would make it the answer. Maybe i needs first up-vote it with arrow up (useful), I am not sure on that – gilhad Jan 15 '16 at 20:15
  • 1
    @John - I think you need to wait a bit before you can accept it as the correct answer. Try checking back in a day or two. – alvits Jan 15 '16 at 20:49
  • Also, while this answers your immediate question, the approaches in the other answers -- a switch statement, or an associative array -- are arguably better ways to achieve what you are trying to do. – tripleee Jan 18 '16 at 10:53
1

You can use an associative array to achieve this:

declare -A lookup=(['foo']='bar' ['x']='y')
echo "${lookup[foo]}" # Prints "bar"
l0b0
  • 55,365
  • 30
  • 138
  • 223
1

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"
Walter A
  • 19,067
  • 2
  • 23
  • 43
  • Thanks Walter. I'm done with this already and working, however your suggestion definitely warrants going back and trying. Stay tuned. – John Jan 21 '16 at 18:46