0

I know that there are variations of this concept, but I haven't found a case where there is a script that takes in a bunch of arguments via flags that is called by another script. How can I avoid duplicate arguments that need to be maintained? Example:

script-to-be-called.sh takes in arguments like this:

function get_user_args() {
  while getopts ":a:b:c:" opt; do
    case ${opt} in
      h )
        usage
        ;;
      a )
        abc=$OPTARG
        ;;
      b )
        efg=$OPTARG-`uuidgen`
        ;;
      c )
        hi=$OPTARG
        ;;
...
      \? )
        echo "Invalid Option: -$OPTARG" 1>&2
        exit 1
        ;;
    esac
  done
  shift $((OPTIND -1))

}

Running it on its own looks like this:

./script-to-be-called -a xyz -b mno -c helloworld

So, now if I have a wrapper script that calls it, how can I avoid having an identical get_user_args function? For example, how can wrapper-script.sh run as shown below where users can pass in arguments in the same way as above without duplicating the get_user_args function.

function generate_parallel_runs() {
  while read params
  do
    echo "./script-to-be-called -a $argA -b $argB -c $argC" >> parallel_commands_list.sh
  done < params.txt

  parallel < "parallel_commands_list.sh"

  #once the parallel runs are created, remove the file
  rm parallel_commands_list.sh
}

Ideally:

./wrapper-script.sh -a xyz -b mno -c helloworld
Howard_Roark
  • 4,088
  • 1
  • 14
  • 24
  • 2
    You have access to all arguments in the special `$@` array, you could use that in the wrapper script to pass them through, as in `./script-to-be-called "$@"`? Also, it looks like the inner script has `getopts` wired up incorrectly, all parameters that take arguments need a `:` after the letter, i.e., `:a:b:c:`. – Benjamin W. Jan 14 '21 at 19:43
  • Ohh I didn't realize you could access the arguments passed via flags in that manner. So, I guess there's nothing unique about this question at all. What you suggested works fine-- users can even pass in `-h` and see the help menu of the script being called – Howard_Roark Jan 14 '21 at 20:42
  • If an admin somehow reads this, feel free to close it unless someone comes up with a better design that I should be considering – Howard_Roark Jan 14 '21 at 20:52
  • Would you say [this](https://stackoverflow.com/q/4824590/3266847) is a good duplicate target? – Benjamin W. Jan 14 '21 at 21:36
  • Ya I think so. I had even read that one before posting this. I just thought there would be a nuance to taking in arguments with flags – Howard_Roark Jan 14 '21 at 21:38

0 Answers0