0

I have two scripts, in which script1 takes pre-fixed arguments as below(I'm using case, shift function to shift arguments and assign them as required) This is how I call script1 normally:

script1 --env [env] --db [db] --table [table] --location [location]

This is how I iterate through arguments in script1:

while [ "$#" -gt 0 ]
do
    case "$1" in
            --env | --environment)
                    shift
                    envie="$1"
                    ;;
            --db)
                    shift
                    db="$1"
                    ;;
            --table)
                    shift
                    table="$1"
                    ;;
           --tableLocation)
            shift
            tableLoc="$1"
            ;;

    esac
   shift
done

A table will be created here after the above code and I'm not specifying the code intentionally.

My second script calls the above script along with the parameters(dynamic) and I have trouble doing it. I'm trying it this way now. I'd like to invoke script1 and would like to consider the status if a table has been created or not( I think a return code would help here).

env=env1
db=database1
table=table1
tablelocation=tablelocation1
#call script1
`source script1 --env $env --db $db --table $table1 --tableLocation $tablelocation`

I'm seeing "command not found" error for this line(there are no syntax errors) Please let me know if there is a way to call script1 with prefixed arguments.

3
  • 3
    Welcome to Stack Overflow. Please read the About and How to Ask pages soon. Are you sure you want to execute the output from sourcing script1? Is the first line of what appears to be script1 actually a synopsis of how to use it? There are aspects of your question that aren't clear. The first script is missing the loop control. It isn't clear what you do with the inputs after you've finished the loop. Please read about how to create an MCVE (minimal reproducible example). Commented May 2, 2017 at 14:17
  • @JonathanLeffler Thank you for your suggestions,I edited my question. the inputs after the loop are intentionally missed. Commented May 2, 2017 at 15:05
  • "Prefixed arguments" means what, exactly? Normally, I understand that to be something like -X--some-arg, where -X is the prefix indicating that --some-arg should be passed through to your subprocess, but that doesn't appear to be the meaning in which you're using that phrase here. Commented May 2, 2017 at 16:21

3 Answers 3

1

You do not need back ticks.

env=env1
db=database1
table=table1
tablelocation=tablelocation1
#call script1
script1 --env "$env" --db "$db" --table "$table1" --tableLocation "$tablelocation"

You use back ticks only, if you want to save the result.

Make sure that script1 is executable (chmod +x) and the directory is your PATH.

Sign up to request clarification or add additional context in comments.

3 Comments

You only use backticks or $(...) if you want to perform command substitution regardless whether you "save" or use the result immediately.
To substitute the value of a command in lieu of its expression.
@DavidC.Rankin Thanks. That is what I meant by "save the result".
1

Your script only processes the first argument/value pair. You need to loop while there are still arguments left, e.g. something like:

while [[ $# -ge 1 ]]; do
    case "$1" in
            --env | --environment)
                    shift
                    envie="$1"
                    ;;
            --db)
                    shift
                    db="$1"
                    ;;
            --table)
                    shift
                    table="$1"
                    ;;
           --tableLocation)
                    shift
                    tableLoc="$1"
                    ;;
           *)
                    echo "invalid argument: $1"
                    exit 1
                    ;;
    esac
    shift  # shift out the consumed value
done

2 Comments

I assume the OP edited the question after you added this answer? (As presently written, the question shows them doing this).
@CharlesDuffy yes, the original question was different
0

If you want to reuse your initial arguments, store them in an array before shifting them off:

initialArgs=( "$@" )

while [ "$#" -gt 0 ]; do
  : ...processing here...
  shift
done

source script1 "${initialArgs[@]}"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.