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 -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 :-)
LVL 14
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.

On one quick look

Are you aware of this typo

 "Y") # Assign OID
                   if [ "$ODI" == "Y" ]; then

ODI instead of OID

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.
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)


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


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


printf "$magenta\n"

Open in new window

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.
RickEpnetAuthor Commented:
Thanks for everything I will use it all.
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.