1

We are doing automation to databases where we have one script is calling another script in the shell. Forex :-

first-script.sh arg1 agr2 arg3 [Suppose 3 arguments are passed, then we have to call second-script.sh 3 times]
#!/bin/bash 
./second-script.sh $1 
./second-script.sh $2 
./second-script.sh $3 

How can we remove this dependency? We want something like if 2 arguments are passed then automatically first script should run like

first-script.sh arg1 agr2
#!/bin/bash 
./second-script.sh $1 
./second-script.sh $2 

and if 5 arguments are passed then, it should run like

first-script.sh arg1 agr2 arg3 arg4 arg5 
#!/bin/bash 
./second-script.sh $1 
./second-script.sh $2 
./second-script.sh $3
./second-script.sh $4 
./second-script.sh $5 

2 Answers 2

2

Use "$@" (or possibly $*) to represent all the arguments:

#!/bin/bash

for arg in "$@"
do
   ./second-script.sh "$arg"
done
Sign up to request clarification or add additional context in comments.

Comments

2

You just need to loop over the arguments.

for arg in $@  ; do 
    second-script.sh "$arg"
done

1 Comment

You for got to quote the variables.

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.