bash shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash shell script
# 1  
Old 08-02-2003
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 ?

Code:
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.


Last edited by norsk hedensk; 08-02-2003 at 12:17 PM..
# 2  
Old 08-02-2003
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.
# 3  
Old 08-02-2003
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.
# 4  
Old 08-02-2003
Quote:
Originally posted by norsk hedensk
i would like to keep everything in functions
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.
# 5  
Old 08-02-2003
ok got it !! Smilie 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 !
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

In Bash shell - the ps -ef shows only the /bin/bash but the script name is not displayed

In Bash shell - the ps -ef shows only the /bin/bash but the script name is not displayed ? Is there any way to get the script names for the process command ? --- Post updated at 08:39 AM --- in KSH (Korn Shell), my command output shows the script names but when run in the Bash Shell... (3 Replies)
Discussion started by: i4ismail
3 Replies

2. Shell Programming and Scripting

Different behavior between bash shell and bash script for cmd

So I'm trying to pass certain json elements as env vars and use them later on in a script. Sample json: JSON='{ "Element1": "file-123456", "Element2": "Name, of, company written in, a very weird way", "Element3": "path/to/some/file.txt", }' (part of the) script: for s... (5 Replies)
Discussion started by: da1
5 Replies

3. Shell Programming and Scripting

Need help with bash shell script

I need to create digit day script that takes a single numeric argument and then it should print out the day of the week using the number modulo 7 formula e.g: 0 - Sunday 6- Saturday 131 - Friday I am fairly new to unix so I don't know how to use the number modulo 7 formula. Does the script need... (3 Replies)
Discussion started by: lukefrost96
3 Replies

4. Shell Programming and Scripting

Bash shell script to check if script itself is running

hi guys we've had nagios spewing false alarm (for the umpteenth time) and finally the customer had enough so they're starting to question nagios. we had the check interval increased from 5 minutes to 2 minutes, but that's just temporary solution. I'm thinking of implementing a script on the... (8 Replies)
Discussion started by: hedkandi
8 Replies

5. Shell Programming and Scripting

Bash Shell Script

HELP!My program ends after entering one choice---need help making it take multiple inputs,instead of terminating after displaying just one #!/bin/bash# Crude address databaseclear # Clear the screen.echo " Contact List"echo " ------- ----"echo "Choose one of the following... (6 Replies)
Discussion started by: help123
6 Replies

6. Shell Programming and Scripting

Help with bash shell script

Hi, I have a file in which records contains non ascii characters. The records are comma delimited and quoted. The non ascii characters are found in a particular column. Example records "YY","AK000021","Ã","IO","PP" "Y1","AK000022","Ã","PO","PP" "Y2","AK000022","Ã","PO","PP" I need to... (2 Replies)
Discussion started by: akshu.agni
2 Replies

7. Shell Programming and Scripting

Bash shell script- help

I need to invoke a program on remote server using ssh in a shell script. In addition i would like to capture date/time and if there is any errors , then script should write to log file. can someone please help me out? (1 Reply)
Discussion started by: sam101
1 Replies

8. Shell Programming and Scripting

Bash Shell script--need help

Hi all, i am beginner to unix and trying out a shell script which does the following. i have to calculate a persons salary. his salary is read from the keyboard. he has two types of deductions. 40% as dearness allowance and 20% as house rent. i have to print the gross salary. here is the code... (5 Replies)
Discussion started by: Irishboy24
5 Replies

9. UNIX for Dummies Questions & Answers

Bash shell script

Hi Guys, I am trying to alter a script for my company. I need the start of it to go something like this. User is asked to input 8 numbers 8 numbers are written to a txt file ***** ***** ***** txt file is read ***** ***** The text file gets read in between other files represented by... (2 Replies)
Discussion started by: outthere_3
2 Replies

10. UNIX for Dummies Questions & Answers

need help with bash shell script

Hi guys! I have just started with shell programming!! I am having pronblem with variable subsitutuion. when i do egrep "*" marks this will give me the pattern match. but how can i catch the output of that result in a variable. if i say result = egrep "*" marks it gives me syntax... (2 Replies)
Discussion started by: vmtailor
2 Replies
Login or Register to Ask a Question