Skip to main content
The second example was actually valid in terms of usual option handling
Source Link
XZS
  • 1.5k
  • 15
  • 27

I have a shell script that accepts a variety of options, some with arguments, some without, some short, some long. It should handle some of these options itself and pass on the rest it does not know how to care about to another program. Can I get getopts or something along its lines to store away unknown arguments?

So for example, say my script is called program and should accept arguments

  • -n with a single argument,
  • --verbose without any argument and
  • -s without any argument.

It parses and prints all its options and their arguments and then calls echo rest: with anything that remains. The following output should be observed.

> program -sin42 --long --verbose
-s
-n with argument 42
--verbose
rest: -i --long 

> program -n --short
-n with argument --short

> program -n
error: -n without argument

Can something like this be achieved in a shell script?

I have a shell script that accepts a variety of options, some with arguments, some without, some short, some long. It should handle some of these options itself and pass on the rest it does not know how to care about to another program. Can I get getopts or something along its lines to store away unknown arguments?

So for example, say my script is called program and should accept arguments

  • -n with a single argument,
  • --verbose without any argument and
  • -s without any argument.

It parses and prints all its options and their arguments and then calls echo rest: with anything that remains. The following output should be observed.

> program -sin42 --long --verbose
-s
-n 42
--verbose
rest: -i --long 

> program -n --short
error: -n without argument

Can something like this be achieved in a shell script?

I have a shell script that accepts a variety of options, some with arguments, some without, some short, some long. It should handle some of these options itself and pass on the rest it does not know how to care about to another program. Can I get getopts or something along its lines to store away unknown arguments?

So for example, say my script is called program and should accept arguments

  • -n with a single argument,
  • --verbose without any argument and
  • -s without any argument.

It parses and prints all its options and their arguments and then calls echo rest: with anything that remains. The following output should be observed.

> program -sin42 --long --verbose
-s
-n with argument 42
--verbose
rest: -i --long 

> program -n --short
-n with argument --short

> program -n
error: -n without argument

Can something like this be achieved in a shell script?

previous edit was inaccurate / does not comply with the OP's question
Link
Totor
  • 21.1k
  • 20
  • 85
  • 114

How to passextract unknown arguments to shellscriptwithin a shell script?

How to collect unknowpass unknown arguments to shellscript?

Source Link
XZS
  • 1.5k
  • 15
  • 27
Loading