How to call a shell script and pass argument from

2019-03-25 14:53发布

问题:

I'm calling a shell script from another shell script and the called script requires some input (command line) parameters.
I'm have below mentioned code, but thats not working. I don't know why the argument values are not passed to the called script.

script1.sh
=======================================
#!/bin/bash
ARG1="val1"
ARG2="val2"
ARG3="val3"
. /home/admin/script2.sh "$ARG1" "$ARG2" "$ARG3"


script2.sh
=======================================
#!/bin/bash
echo "arg1 value is: $1 ....."
echo "arg2 value is: $2 ....."
echo "arg3 value is: $3 ....."

But when I execute script1.sh I get following result:

arg1 value is:  .....
arg2 value is:  .....
arg3 value is:  .....

What am I missing?

回答1:

By sourcing the second script with . /home/admin/script2.sh, you're effectively including it in the first script, so you get the command line arguments to the original script in $@. If you really want to call the other script with arguments, then do

/home/admin/script2.sh "$ARG1" "$ARG2" "$ARG3"

(make sure it's executable).



标签: shell unix