RickEpnet
asked on
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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)
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"
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.
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.
ASKER
Thanks for everything I will use it all.
ASKER