0
#!/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
4
  • please update the question with an example of what's in the `currentState' variable Commented Jan 29, 2021 at 20:53
  • 1
    [[ $(yourCommand) = *2 ]], if you want to check if the output ends with 2 (in a bash-only way; adding an answer describing the portable-to-all-POSIX-shells approach). Commented Jan 29, 2021 at 20:54
  • 1
    Your question is confusing. First you ask about rewriting the if statement, but then your attempt are about rewriting the variable assignments. Commented Jan 29, 2021 at 20:54
  • BTW, note that == shouldn't be used in [ (which is another name for the test command). 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). Commented Jan 29, 2021 at 20:58

3 Answers 3

3

In code compatible with all POSIX-compliant shells:

case "$(snmpget -v 1 -Oe -c "$community" "$ip" "PowerNet-MIB::sPDUOutletCtl.$1")" in
  *2) sfx=On;;  # output from snmpget ends with 2
  *)  sfx=Off;; # output from snmpget does not end with 2
esac
Sign up to request clarification or add additional context in comments.

1 Comment

The bash specfic analog is if [[ "$(snmpget ...)" == *2 ]]; then ... -- the key here is that == within [[...]] is a pattern matching operator.
1

You can't use the parameter expansion operators like this, they only operate on parameters (i.e. variables).

You can pipe to the tail command to get the last character of output.

currentState="$(snmpget -v 1 -Oe -c abcd 172.16.0.28 PowerNet-MIB::sPDUOutletCtl.1 | tail -c 2)"

You need to use -c 2 because the last character is a newline, and you want the character before that. The command substitution will then discard the newline at the end of the tail output.

Comments

0

Assumptions:

  • currentState ends in a character that's expected to be a number (or at least if the last character is 2 then the snmpget call should end with ...On)
  • the objective is to determine if the last snmpset arg should end with ..On or ..Off

One idea:

ip="172.16.0.28"
community="abcd"

currentState=$(snmpget -v 1 -Oe -c $community $ip PowerNet-MIB::sPDUOutletCtl.$1)
currentState="${currentState: -1}"

# figure out the suffix ...

sfx='Off'
[[ "${currentState}" = "2" ]] && sfx='On'

# now make the snmpset call
snmpset -v 1 -c "${community}" "${ip}" PowerNet-MIB::sPDUOutletCtl.$1 i outlet"${sfx}"

3 Comments

I thought the question was about combining the smtpget with the line that gets the last character.
ahhh, missed that twist; I was (obviously) looking at how to collapse the current if/then/else into a single if test
The question is confusing about what he really wants.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.