bash Script

I have this practice script I am working on. (See attachment) call test

It does not see the option Y as the letter Y. (I know the option and the variable are the same just a coincidence.)

I use this commend line

sudo ./test -C public -H 10.10.10.48 -Y Y

I want when the -Y option has a Y for the lastd variable to be 4.

I am a total beginner at bash scripting please be easy on me :-)
script1.txt
LVL 14
RickEpnetAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

farzanjCommented:
On one quick look

Are you aware of this typo

 "Y") # Assign OID
                        OID="$OPTARG"
                   if [ "$ODI" == "Y" ]; then
                       lastd="4"
                  fi

ODI instead of OID
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RickEpnetAuthor Commented:
Well that was an easy 500 Points. Thank you soooo much. Sometimes you just cannot see it.
0
TintinCommented:
Apart from the OID typo and a missing : on the options, the script was fine.

Here's the corrected script (with some basic error checking)

#!/bin/bash

while getopts "C:H:Y:" OPTION
do
  case $OPTION in
  C)  COMMUNITY="$OPTARG" 	# Assign Community
      ;;
  H)  HOST_NAME="$OPTARG"	# Assign hostname
      ;;
  Y)  OID="$OPTARG"		# Assign OID
      if [ "$OID" == "Y" ]
      then
          lastd=4
      fi
      ;;
  *)  echo "Usage: $0  -C <community> -H <host> -Y <OID>" >&2
      exit 1
      ;;
  esac

done


function magenta {
   magenta=$(snmpwalk -v1 -Ovq -c $COMMUNITY $HOST_NAME .1.3.6.1.2.1.43.11.1.1.9.1.$lastd)

   EXIT_STRING="$magenta"
}

magenta
printf "$magenta\n"

Open in new window

0
TintinCommented:
To further explain the role of the : in getopts, it means that the argument is mandatory.

In your original script, it would allow you to do:

script -Y

By specifying

getopts "C:H:Y:" OPTION

if you try that again, you'll get

./scriptname:  option requires an argument -- Y


BTW, best not to name your scripts 'test', as that clashes with the /usr/bin/test executable and can lead to confusion depending on how you invoke your script.
0
RickEpnetAuthor Commented:
Thanks for everything I will use it all.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.