I have been researching on using bash scripts to process command-line arguments. I have multiple optional arguments, each of which have one or more operands. An example is:
./script.sh -f file1 file2 -s server1 server2
-f
itself is optional, but must be followed by filename; -s
is optional, can be used without any operands or operands.
I know I can force putting "" on operands so I only deal with arguments with one operand and can use case $1 $2 shift
to process it.
But I am interested in doing so without quotes, just to save some typing for the users.
A rough idea would be read in "$@"
as one string, and separate them by space, then locate arguments with -/--
and assign operands following them. Maybe I can use an array to do that?
Any suggestions would be welcome.