bash shell script

im trying to teach my self bash shell scripting. i want to include command line arguments in my script i am writing for practice. when i run my script it dosnt make a difference what arguments there are, it just out puts the error message as if there were none. here is the check_opts () function in my script, can someone tell me whats wrong here ?

check_ops () # lets see what happens ...
{
NO_ARGS=0
E_OPTERROR=65

if [ $# -eq "$NO_ARGS" ] # should check for no arguments
then
	echo "Usage: `basename $0` <OPTIONS> -s <HOSTNAME> "
	echo "Try './serverstatus -h' for more information."
	exit $E_OPTERROR
fi

while getopts ":invhs:" Option
do
	case $Option in
		i )
			interactivemode_func
		;;

		n )
			non_interactivemode_func
		;;

		v )
			version_func
		;;

		h )
			help_func
		;;

		s )
			echo "will take hostname" # dunno how to do this hold on
		;;

		* )
			echo "Unimplemented option chosen"
		;;
	esac
done

shift $(($OPTIND - 1))
}

this is called by a main () function that is ran before anything. thanks !

edit: for option -s i will be setting the hostname that will be portscanned. usage would look like this:

./serverstatus -s hostname

if that is the only option i will want it to default to non interactive mode. i can post more of my script if someone needs to see what i am doing. it is all very simple, i run nmap <hostname> and set the output to $a

right now that is the first thing that is done with my server address right in the script. then all i do is grep $a for corresponding ports and echo if that service is running or not.

A function can be passed arguments. Your code is going to carefully examine the arguments passed to check_ops when it was invoked. Apparently you aren't passing any arguments to check_ops.

im learning as i go, my only reference is the advanced bash-scripting guide at tldp.org .

i cant find how to pass the arguments to my check_ops() function. would it be easier to instead of check_ops() being a function it would just be the first part of the script ? i cant rigt now but that is what i am going to try when i get a chance. i would like to keep everything in functions however but this should be the first thing run anyway, so thats probably how i will do it, if that works. well i will post back if i figured it out and how i did it. thanks for the help perderabo.

You would have an easier time if you would adapt to the language rather than fighting it.

I just tested bash and it seems to have adopted ksh's implementation of $@. So you should be able to pass the args with
check_ops "$@"

Of course, in your case, that will pass the args of main to check_ops.

ok got it !! :slight_smile: thanks perderabo. what i did was get rid of the check_ops function and put that in main, all main did was call check_ops anyway, and from there i will go. thanks again !