简体   繁体   English

如何使用参数传递多个Shell脚本

[英]How to multiple shell scripts passing with arguments

I have scripts a.sh and b.sh in which i have pass the ip as argument. 我有脚本a.sh和b.sh,其中我已将ip作为参数传递。

I tried running it as 我尝试以

sh -x a.sh 172.19.57.21 & b.sh 172.19.57.21 &

But I see only first script runs. 但是我只看到第一个脚本运行。

When you run sh -x a.sh 172.19.57.21 & b.sh 172.19.57.21 & : 当您运行sh -x a.sh 172.19.57.21 & b.sh 172.19.57.21 &

  • sh -x a.sh 172.19.57.21 is one command, & sends it to background immediately sh -x a.sh 172.19.57.21是一个命令, &立即将其发送到后台

  • b.sh 172.19.57.21 is another command, again & puts it in background b.sh 172.19.57.21是另一个命令,再次&把它放在后台

The problem seems to me is that the script b.sh is not executable and as you are not running it as an argument to shell (unlike a.sh ), it is failed in the PATH search. 在我看来,问题是脚本b.sh不可执行,并且由于您未将其作为shell的参数运行(与a.sh不同),因此PATH搜索失败。

You can run b.sh as shell's argument as well eg: 您也可以运行b.sh作为shell的参数,例如:

sh a.sh 172.19.57.21 & sh b.sh 172.19.57.21 &

Or if both scripts are executables and have proper shebang: 或者,如果两个脚本都是可执行文件,并且具有适当的shebang:

./a.sh 172.19.57.21 & ./b.sh 172.19.57.21 &

I would recommend a wrapper to get the argument IP address once, and call required scripts from the wrapper, something like a tiny function would do: 我建议使用包装器一次获取参数IP地址,然后从包装器中调用所需的脚本,就像一个微型函数一样:

wrapper() {
    /path/to/a.sh "$@" &
    /path/to/b.sh "$@" &
}

Now, you can just do eg: 现在,您可以执行例如:

wrapper 172.19.57.21

use ; 使用; between commands like 在命令之间

sh -x a.sh 172.19.57.21 &; sh -x b.sh 172.19.57.21 &

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

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