How to extract unknown arguments within a shell script?
3
votes
3
answers
6463
views
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?
Asked by XZS
(1488 rep)
Apr 28, 2015, 09:18 PM
Last activity: May 3, 2024, 04:40 PM
Last activity: May 3, 2024, 04:40 PM