0

I have two scripts: aaa and bbb.

Each script has some arguments. Argument can be string, number or has format like --some-action or like --name=foo.

Script aaa execute script bbb. Some arguments for bbb generated in aaa, but some arguments I can manually add from executing script aaa like:

$ aaa "arguments for aaa" "arguments for bbb"

I can not pass to bbb all arguments of aaa, because bbb has control to unknown arguments.

I do not know how extract "arguments for bbb" in aaa script. Can anybody help me?

So, I want to execute in aaa:

bbb "some arguments from aaa" "arguments for bbb"
  • How exactly will the arguments be laid out? Will they actually be in quotes like your example: aaa "arguments for aaa" "arguments for bbb"? Or would it be more like this: aaa -a -b -c -d -e -f where only -d -e -f are used in bbb? – Peschke Apr 11 '17 at 15:38
  • Typically bbb "$@" will pass the arguments supplied to aaa along. – thrig Apr 11 '17 at 15:42
  • @Peschkem second way: aaa -a -b -c -d -e -f. Now I use argument-separator to detect where arguments for bbb started. Now I'm use way: for arg in "$@"; do case ... and increment argument index (argIdx) to detect, where first argument for bbb. I'm use ${@:$argIdx} at finish. – Yura Shinkarev Apr 11 '17 at 15:50
0

You could parse all arguments in the aaa script with getopt and then pass them to your script as needed.

I adjustet an example:

#!/bin/bash

OPTS=`getopt -o abcdef -l argument-for-both: -- "$@"`
if [ $? != 0 ]
then
    exit 1
fi

eval set -- "$OPTS"

while true ; do
    case "$1" in
        -a) OPT_a=true; shift;;
        -d) ARG_B="$ARG_B -b"; shift;;
        --argument-for-both)
            OPT_argument_for_both=true
            ARG_B="$ARG_B --argument-for-both"
            shift;;
    esac
done

# aaa
if [[ $OPT_a ]] ; then do_something ; fi
if [[ $OPT_argument_for_both ]] ; then do_something_else ; fi

# bbb
bbb $ARG_B

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.