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.
-sif./script.sh -f -sthinks-sis the start of a new option?-f file1 -f file2) for each argument. Also, what does-swithout an option mean? Consider making a separate zero-argument option for the no-argument case, then make-sa repeatable one-argument-required option.-sto be passed as a positional argument without any prefix,./or otherwise. If one passes--, a parser complying with POSIX guidelines will treat that as "end of options", meaning that all future arguments are positional and should be parsed as such. See POSIX utility syntax guidelines, guideline #10.