[bash] getopts not executing when called second time.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [bash] getopts not executing when called second time.
# 1  
Old 02-18-2010
[bash] getopts not executing when called second time.

Hi,

Unexpectedly, the function below doesn't seem to work
when called a second time. The output shows that when
the function is called the first time, it works as expected
but when it is called a second time, the loop that processes
the options passed to the function, with the while loop,
are completely bypassed.

Thanks in advance.

Code:
#!/bin/bash
function fn_check
{
	echo; echo "in check"
	while getopts ":defhrwx" opt; do
	echo "in loop"
	case $opt in
	d)	echo "in d $2";;
	e)	echo "in e";;
	f)	echo "in f";;
	h)	echo "in h";;
	r)	echo "in r";;
	w)	echo "in w";;
	x)	echo "in x";;
	\?)	echo "in ?";;
	esac
	done
}
fn_check -edrw helloworld
fn_check -erwx helloworld

OUTPUT.
Code:
in check
in loop
in e
in loop
in d helloworld
in loop
in r
in loop
in w

in check

# 2  
Old 02-18-2010
It keeps track of what argument to read next with OPTIND, which gets set globally. Try:

Code:
#!/bin/bash
function fn_check
{
        local OPTIND
        local OPTARG
	echo; echo "in check"
	while getopts ":defhrwx" opt; do
	echo "in loop"
	case $opt in
	d)	echo "in d $2";;
	e)	echo "in e";;
	f)	echo "in f";;
	h)	echo "in h";;
	r)	echo "in r";;
	w)	echo "in w";;
	x)	echo "in x";;
	\?)	echo "in ?";;
	esac
	done
}
fn_check -edrw helloworld
fn_check -erwx helloworld

This should stop it from modifying the global OPTIND value.

You should be able to use ${OPTARG} instead of $2 -- indeed it's preferable to do that instead of hardcoding a parameter as position 2 -- and I'm not sure why that doesn't work.

Last edited by Corona688; 02-18-2010 at 01:24 PM..
# 3  
Old 02-18-2010
Thanks for your help, done the trick.

The way I understand it that $2 is not park of the options
string but a separate parameter after the options. As long
as I don't use any OPTARG's with the options it should be
okay. But as soon as I start adding them the positions of
the input parameters could well change.

Thanks again.

Last edited by ASGR; 02-18-2010 at 01:43 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk call in bash function called with arugments not working, something lost in translation?

Hello, I have this awk code in a bash script to perform a find and replace task. This finds one unique line in a file and substitutes the found line with a replacement. #! /bin/bash # value determined elsewhere total_outputs_p1=100 # file being modified... (5 Replies)
Discussion started by: LMHmedchem
5 Replies

2. Shell Programming and Scripting

Executing bash file with sudo for the second time, leads to permission denied, for some commands

I have a script that checks if the script has been ran with sudo. If the script is not ran as sudo, the current script is being executed with exec sudo bash. You are asked for a password, you type in the password, success. Everything is perfect - the commands inside the script are ran as sudo.... (1 Reply)
Discussion started by: boqsc
1 Replies

3. UNIX for Advanced & Expert Users

[BASH] Getopts/shift within a function, unexpected behaviour

Hello Gurus :) I'm "currently" (for the last ~2weeks) writing a script to build ffmpeg with some features from scratch. This said, there are quite a few features, libs, to be downloaded, compiled and installed, so figured, writing functions for some default tasks might help. Specialy since... (3 Replies)
Discussion started by: sea
3 Replies

4. Shell Programming and Scripting

Calling bash script works when called manually but not via Cron?

Hi, I've got a Bash backup script I'm trying to run on a directory via a cron job nightly. If I ssh in and run the script manually it works flawlessly. If I set up the cron to run evertything is totally messed up I don't even know where to begin. Basically the path structure is ... (6 Replies)
Discussion started by: wyclef
6 Replies

5. Shell Programming and Scripting

Bash script from makefile - it is called each time i call make

I've created a tag in the makefile: mytag: $(shell ${PWD}/script.sh) When i do: make clean - the script is executed When i perform make or make mytag the script is again executed with the output: make: Nothing to be done for mytag What i want ? I want script.sh to be executed only... (0 Replies)
Discussion started by: Pufo
0 Replies

6. Shell Programming and Scripting

[BASH] getopts, OPTARG is passed but empty

EDIT: -- SOLVED -- Heyas, Getting used to optargs, but by far not understanding it. So i have that script that shall be 'changeable', trying to use the passed arguments to modify the script visuals. Passing: browser -t test -d sect $HOME Where -t should change the title, and -d... (0 Replies)
Discussion started by: sea
0 Replies

7. Shell Programming and Scripting

[BASH] Using getopts

Heyas Just recently there was a thread about parsing arguments, where i read the first time about getopts. This said, i'd like to 'provide' a list function that can be 'trigered' using an 'option'(?). The regarding code snippets are: while getopts... (7 Replies)
Discussion started by: sea
7 Replies

8. Shell Programming and Scripting

bash:getopts command help

How can I say one of the options is required? can I use an if statement? let say: while getopts ":c:u:fp" opt; do case $opt in c) echo "-c was triggered, Parameter: $OPTARG" >&2;; u) echo "-u was triggered, Parameter: $OPTARG" >&2;; f) echo "-u was triggered,... (2 Replies)
Discussion started by: bashily
2 Replies

9. Shell Programming and Scripting

executing with system time

hi, i have written an shell script which reads an folder and write the filenames to another file: here is the script: #!/bin/bash find /var/www/vhosts/remixland.de/web_users/stream001/jingle15 -type f -name "*.mp3" > /etc/shoutcast001/example.lst killall -USR1 sc_trans_linux001... (1 Reply)
Discussion started by: maydo
1 Replies

10. Shell Programming and Scripting

executing a script for a certain amount of time

I am writing a script that takes two parameters: the name of another script and an integer that represents a number of seconds. The script must execute the second script (first parameter) for the specified number of seconds (second parameter), suspend it for the same number of seconds, and continue... (9 Replies)
Discussion started by: ponchorage
9 Replies
Login or Register to Ask a Question