简体   繁体   English

如何调用shell脚本并从另一个shell脚本传递参数

[英]How to call a shell script and pass argument from another shell script

I'm calling a shell script from another shell script and the called script requires some input (command line) parameters. 我从另一个shell脚本调用shell脚本,并且被调用的脚本需要一些输入(命令行)参数。
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: 但是当我执行script1.sh时,我得到以下结果:

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

What am I missing? 我错过了什么?

By sourcing the second script with . /home/admin/script2.sh 通过采购第二个脚本. /home/admin/script2.sh . /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 $@ . . /home/admin/script2.sh ,你有效地将它包含在第一个脚本中,所以你得到$@原始脚本的命令行参数。 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). (确保它是可执行的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM