我有一个以这样开头的bash脚本:
#!/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}')
我想要做的是,如果$ 2命令行参数的某人进入 operatingSystemMemoryStatus ,如何在脚本中为它选择预定义的值?因此,在RESULT中引用了上面的$ 2,如何使用 .1.3.6.1.4.1.674.10909.1.400.20.1.4 值获得该命令?
所以,如果我进入
check_snmp 192.168.0.1 operatingSystemMemoryStatus public script
会做:
/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
我该如何做到这一点?
答案 0 :(得分:1)
我认为,您希望使用这样的间接扩展:
#!/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}')
并称之为
./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>
在man bash
中,您可以阅读
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.
答案 1 :(得分:1)
您可以使用关联数组来实现此目的:
declare -A lookup=(['foo']='bar' ['x']='y')
echo "${lookup[foo]}" # Prints "bar"
答案 2 :(得分:1)
使用开关结构。我为所有选项添加了一个3个字母的替代选项:
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"
我刚刚复制/粘贴了这些值。不是最好的方法,您可以引入变量来简化比较和更新值:
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"