I'm just not sure where to start looking into this. I want to be able to create switches for my script. Back in the day I'd make my scripts interactive...you know:
echo "what report do you want"
echo "A)boxes with errors"
echo "B)boxes with more than 5 errors"
echo "C)Service groups that have thrown errors"
read $1
if ...
but now that seems kind of amateurish. I'd rather have a script where errorreport.sh -c made a list of boxes, errorreport.sh -m listed boxes with multiple errors and so on.
I'm not asking anyone to give me a step by step on how to do this, I'd rather learn by doing, but I don't even know where to start. I've tried googling "adding switches to bash scripts" and the like, and I've been scanning my o'reiley books, but at quick glance I don't see what I'm looking for.
If anyone could tell me where I might at least start reading up on this I'd appreciate it.
You can do something as the following, you can also add additional logic to force a user to enter good input, so the script does not exit, but I'll leave that to you.
#!/bin/sh
echo "What report do you want?"
echo "A) boxes with errors"
echo "B) boxes with more than 5 errors"
echo "C) Service groups that have thrown errors"
read CHOICE
case $CHOICE in
A|a)
echo "Choice was $CHOICE"
;;
B|b)
echo "Choice was $CHOICE"
;;
C|c)
echo "Choice was $CHOICE"
;;
*)
echo "Valid Choices are A,B,C"
exit 1
;;
esac
This might also help you..Look out for $1=-g option.
# # # # # # # # # # # #
# main section
# # # # # # # # # # # #
print "script $(basename $0) executing $(date +%Y/%m/%d\ %H:%M:%S) \n"
#
# only allow this script to be run by the root user
#
if ! id | grep -q "uid=0(root)" ; then
print "ERROR: must be root in order to run this script"
exit 1
fi
if [[ "$PLEX_ROOT" = "/claims/amxw" ]]; then
if [[ "$NLROOTDIR" != "/claims/amxw" ]]; then
print "ERROR: NLROOTDIR must be set to /claims/amxw in order to create a plex"
exit 1
fi
fi
if [[ "$1" = "-g" ]]; then
shift
print "You are obviously a plex god and have chosen to skip the approved naming convention. Please proceed, your eminence"
print
mode=GOD
else
mode=MORTAL
fi
plexname=$1
if [[ -z $plexname ]]; then
print "ERROR: plex name isn't specified"
exit 1
fi
if [[ "$mode" = "MORTAL" ]]; then
if ! print $plexname | grep -q ^plex ; then
print "ERROR: plex name must begin with 'plex' and be at least 9 characters"
exit 1
fi
fi