Using arguments

I have a SNMP agent that sends three arguments to the script to get a value at the end. The first is the LeafNumber, second is the request type (SET, GET, GETNEXT), and the last is a string that represents some value to be set(used only for set requests).

The agent string looks like this:

extension 1 OctetString readonly /etc/motdpass.sh

So it makes the first agument variable(ARGV)=1
The second would be "GET" or "GETNEXT"

In the manual for the SNMP agent it gives a $ARGV[0,1, or 2] in their perl script example, but how would this look in a shell script? Would it just be $1, $2, or $3? This is what I have so far.

#!/bin/sh
# Shell script to pull primary ESA out of a file

FILENAME= /etc/motd

# Do work
extPrimEsa= grep "Primary ESA" $FILENAME|cut -d\= -f2

if [ $ARGV[0] -eq 1 ]; then 
	# OID queried is 1.3.6.1.4.1.546.14.1.0
	if [ $ARGV[1] = "GET" ]; then
	echo extPrimEsa
	fi
fi

I need to go through 8 different variables, but once I have this one down and working, I'll add the rest in. Am I doing this right?

Hi, for starters, you could try something like this:

#!/bin/sh
# Shell script to pull primary ESA out of a file

FILENAME=/etc/motd   # No space in assignments

if [ $1 -eq 1 ]; then
  if [ "$2" = "GET" ]; then
    # Do work
    extPrimEsa=$(grep "Primary ESA" "$FILENAME"|cut -d\= -f2)
    echo "$extPrimEsa"
  fi
fi
1 Like

This is great, but there were a few tweaks. The final code for this one variable looks like this:

#!/bin/sh
# Shell script to pull primary ESA out of a file

FILENAME=/etc/motd   # No space in assignments

if [ $1 -eq 1 ]; then
  if [ "$2" = "GET" ]; then
    # Do work
    extPrimEsa= grep "Primary ESA" "$FILENAME"|cut -d\= -f2
    echo "$extPrimEsa"
  fi
fi

Thank you so much.

---------- Post updated at 01:53 PM ---------- Previous update was at 09:45 AM ----------

Okay, this is my final final script that has all the extension variables accounted for and are being passed through SNMP to the server (Spectrum).

#!/bin/sh
# Shell script to pull primary ESA out of a file
# Define some stuff

FILENAME=/etc/motd

if [ "$1" = 1 ]; then
	extPrimEsa=grep "Primary ESA" "$FILENAME"|cut -d\= -f2
	echo "$extPrimEsa"

elif [ "$1" = 2 ]; then
	extSecEsa=grep "Secondary ESA" "$FILENAME"|cut -d\= -f2
	echo "$extSecEsa"
 	
elif [ "$1" = 3 ]; then
	extPrimCust=grep "Primary Customer" "$FILENAME"|cut -d\= -f2
	echo "$extPrimCust"

elif [ "$1" = 4 ]; then
	extPrimDBA=grep "Primary DBA" "$FILENAME"|cut -d\= -f2
	echo "$extPrimDBA"

elif [ "$1" = 5 ]; then
	extSysLoc=grep "System Location" "$FILENAME"|cut -d\= -f2
	echo "$extSysLoc"

elif [ "$1" = 6 ]; then
	extBusUnt=grep "Business Unit" "$FILENAME"|cut -d\= -f2
	echo "$extBusUnt"

elif [ "$1" = 7 ]; then
	extConsl=grep "Console" "$FILENAME"|cut -d\= -f2
	echo "$extConsl"

elif [ "$1" = 8 ]; then
	extSysTyp=grep "System Type" "$FILENAME"|cut -d\= -f2
	echo "$extSysTyp"

fi

Whenever you find yourself writing the same 4 lines 9 times running, there's probably a better way. :slight_smile: I'd suggest a case statement:

case "$1" in
1)    VAR1=$(grep "Primary ESA")  ;;
2)    VAR2=$(grep "Secondary ESA")  ;;
3)    VAR3=$(grep "Primary Customer") ;;

...

*)   echo "Unknown option $1" >&2 ;;
esac < "$FILENAME"

You can redirect in $FILENAME at the bottom that way, instead of repeating it over and over. Without a filename, grep just reads from standard input, which is what's being redirected at the bottom of the case statement there.