Question

What I want to do is something like

$ ./my-script -a file1 file2 file3 -d file4 file5 file6 -r file7 file8

or

$ ./my-script -a *.txt -d src1/*.tex sr2/*.tex

However, it seems getopts only support zero or one argument per flag.

Is there a good way to handle this requirement? I'm currently parsing the argument by myself..

Was it helpful?

Solution

Specify a single comma-delimited (or some other delimter) argument for each option:

$ ./my-script -a file1,file2,file3 -d file4,file5,file6 -r file7,file8

Then split the individual arguments after you parse the options. One way to do that:

IFS=, read a1 a2 a3 <<< $a_argument

or

IFS=, read -a a <<< $a_argument

Another option is to quote the argument, and let the script do the expansion:

./my-script -a '*.tex' -d '*.pdf'
$ ./my-script -a '*.txt' -d 'src1/*.tex sr2/*.tex'

where in the script:

#!/bin/bash

as=( $a_argument )
ds=( $d_argument )

This is similar to how find handles, e.g, the -name primary.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top