Bash, Getopts and functions

StumbleUpon Toolbar | No Comments

Helpful hint, if you have a bash script with a function calling getopts, i.e.

make_tarball() {
    local START=`get_start_time`
    ## declare some variables
.....
    local OPTARG OPTIND
    ## Parse the args
    while getopts "s:d:a:x:X:v" ARG
    do
	case $ARG in
	    s) SOURCE_PATH="$SOURCE_PATH $OPTARG"
		;;
....
        esac
   done

you MUST make OPTIND a local variable, or a second (third, etc) call to the function will fail, with OPTIND too large to see any of the arguments

blegh
A

Leave a comment

About this Entry

This page contains a single entry by Aaron Macks published on July 9, 2012 1:45 PM.

Lang & Heyne and Ivory Watches was the previous entry in this blog.

Kaleidoscopic Watertower is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

OpenID accepted here Learn more about OpenID