首页 > 解决方案 > 基于没有传递给第一个脚本的参数在 shell 脚本中调用 shell 脚本

问题描述

我们正在对数据库进行自动化操作,其中一个脚本在 shell 中调用另一个脚本。外汇:-

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 

我们怎样才能消除这种依赖呢?我们想要这样的东西,如果传递了 2 个参数,那么第一个脚本应该自动运行

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

如果传递了 5 个参数,它应该像这样运行

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 

标签: linuxshellscripting

解决方案


你只需要遍历参数。

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

推荐阅读