#!/bin/bash
ip="172.16.0.28"
community="abcd"
currentState=$(snmpget -v 1 -Oe -c $community $ip PowerNet-MIB::sPDUOutletCtl.$1)
currentState="${currentState: -1}"
if [ $currentState == 2 ] ; then
snmpset -v 1 -c abcd 172.16.0.28 PowerNet-MIB::sPDUOutletCtl.$1 i outletOn
else
snmpset -v 1 -c abcd 172.16.0.28 PowerNet-MIB::sPDUOutletCtl.$1 i outletOff
fi
Is it possible to rewrite the if statement as an one liner?
Something like
echo "${$(snmpget -v 1 -Oe -c abcd 172.16.0.28 PowerNet-MIB::sPDUOutletCtl.1): -1}"
bash: ${$(snmpget -v 1 -Oe -c abcd 172.16.0.28 PowerNet-MIB::sPDUOutletCtl.1): -1}: bad substitution
does not work.
EDIT: As requested, a possible return of snmpget:
$ snmpget -v 1 -Oe -c abcd 172.16.0.28 PowerNet-MIB::sPDUOutletCtl.1
PowerNet-MIB::sPDUOutletCtl.1 = INTEGER: 1
[[ $(yourCommand) = *2 ]], if you want to check if the output ends with2(in a bash-only way; adding an answer describing the portable-to-all-POSIX-shells approach).ifstatement, but then your attempt are about rewriting the variable assignments.==shouldn't be used in[(which is another name for thetestcommand). The only standard-compliant string comparison operator is=, not==; bash allows==as an extension, but using it means your code won't work with other shells (like ash or dash).