LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-24-2009, 02:59 PM   #1
dkrysak
LQ Newbie
 
Registered: Sep 2004
Distribution: centos
Posts: 25

Rep: Reputation: 15
Exclamation BASH - problem with subsequent calls to function with getopts


Hi there,

In an attempt to understand getopts better, I wrote a simple function that is a wrapper for rsync. basic stuff.

Now i am trying to make my "-s" a required switch no matter what,

Now when i run a script that calls my below function, the first run goes fine, but then the second instance of my function errors out with the "You have not set the required -s option".

Now if you look in the function, I do have spot that checks for the existence of the -s option..... but from looking at the calling script, you can see that it is there.

Ideas?


FUNCTION

Code:
function dosync() {
	SRC=
	d=0
	p=0
	s=0
	while getopts "hdp:s:" OPTION
	do
		case $OPTION in

		d)
			DEL=$OPTARG
			d="1"
			;;
		h)
			usage
			exit 1
			;;
		p)
			PORT=$OPTARG
			p="1"
			;;
		s)			
			SRC=$OPTARG
			s=1
			;;
		?)
			usage
			exit 1
			;;
		*)
			usage
			exit 1
			;;
	     esac
	done
	
	if [ $s -ne "1" ]; then
		echo "You have not set the required -s option"
		exit 1
	fi
	
	if [[ "$d" -eq "1" && "$p" -eq "1" ]]; then
		rsync -av --delete -p $PORT -e ssh  $ID@$IP:$SRC $BUFLD
	elif [[ "$d" -eq "1" && "$p" -ne "1" ]]; then
		rsync -av --delete -e ssh  $ID@$IP:$SRC $BUFLD
	elif [[ "$d" -ne "1" && "$p" -eq "1" ]]; then
		rsync -av -p $PORT -e ssh  $ID@$IP:$SRC $BUFLD
	elif [[ "$d" -ne "1" && "$p" -ne "1" ]]; then
		rsync -av -e ssh  $ID@$IP:$SRC $BUFLD
	else
		echo "There seems to be an error with your command"
		exit 1
	fi
}
CALLING (the function) SCRIPT

Code:
dosync -s /some/path
dosync -s /some/other/path
dosync -s /some/path2
dosync -s /some/other/path2
 
Old 03-25-2009, 08:47 AM   #2
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
The variable OPTIND needs to be reset to 1 between each call:

Code:
dosync -s /some/path
OPTIND=1
dosync -s /some/other/path
OPTIND=1
dosync -s /some/path
OPTIND=1
dosync -s /some/path
OPTIND=1
 
Old 03-25-2009, 08:50 AM   #3
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
Take a look at the paragraph before the OPTIONS section:

http://www.opengroup.org/onlinepubs/...s/getopts.html
 
Old 03-25-2009, 03:28 PM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,451
Blog Entries: 54

Rep: Reputation: 2895Reputation: 2895Reputation: 2895Reputation: 2895Reputation: 2895Reputation: 2895Reputation: 2895Reputation: 2895Reputation: 2895Reputation: 2895Reputation: 2895
Quote:
Originally Posted by crabboy View Post
The variable OPTIND needs to be reset to 1 between each call
Awesome! I've been staring at this but didn't check *that* getopts var.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] bash : getopts problem in bash script. angel115 Programming 2 03-02-2009 10:53 AM
function calls inside a function manas_sem Programming 2 02-28-2007 01:27 AM
Tracing function calls... How? ik_nitk Programming 3 06-05-2006 07:05 AM
libc function calls h/w Programming 4 02-13-2004 03:00 PM
using timer for repeated function calls poojamanu Programming 1 04-09-2003 08:17 PM


All times are GMT -5. The time now is 03:15 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration