Alex Rivera | Logout

Is mixing getopts with positional parameters possible?

Asked 2012-07-31T14:58:57.307
105

I want to design a shell script as a wrapper for a couple of scripts. I would like to specify parameters for myshell.sh using getopts and pass the remaining parameters in the same order to the script specified.

If myshell.sh is executed like:

myshell.sh -h hostname -s test.sh -d waittime param1 param2 param3

myshell.sh param1 param2 -h hostname param3 -d waittime -s test.sh

myshell.sh param1 -h hostname -d waittime -s test.sh param2 param3

All of the above should be able to call as

test.sh param1 param2 param3

Is it possible to utilize the options parameters in the myshell.sh and post remaining parameters to underlying script?

Edit
Report

1 Answer

-1

There are some standards for unix option processing, and in shell programming, getopts is the best way of enforcing them. Almost any modern language (perl, python) has a variant on getopts.

This is just a quick example:

command [ options ] [--] [ words ]
  1. Each option must start with a dash, -, and must consist of a single character.

  2. The GNU project introduced Long Options, starting with two dashes --, followed by a whole word, --long_option. The AST KSH project has a getopts that also supports long options, and long options starting with a single dash, -, as in find(1) .

  3. Options may or may not expect arguments.

  4. Any word not starting with a dash, -, will end option processing.

  5. The string -- must be skipped and will end option processing.

  6. Any remaining arguments are left as positional parameters.

The Open Group has a section on Utility Argument Syntax

Eric Raymond's The Art of Unix Programming has a chapter on traditional unix choices for option letters and their meaning.

answered 2012-07-31T22:30:20.047

Your Answer