bash-将脚本作为另一个脚本的参数传递 [英] bash - pass script as argument of another script
问题描述
我找不到类似的问题.
如何正确地将bash脚本作为参数传递给另一个bash脚本.
How can I properly pass a bash script as an argument to another bash script.
例如,假设我有两个脚本,每个脚本都可以接受许多参数,我想将一个脚本作为另一个脚本的参数传递.像这样:
For example, let's say I have two scripts that can each accept a number of parameters, I want to pass one script as the argument of the other. Something like:
./script1 (./script2 file1 file2) file3
在上面的示例中,script2将file1和file2合并在一起,并回显一个新文件,但这与问题无关.我只想知道如何传递 script2
作为参数,即正确的语法.
In the above example, script2 merges file1 and file2 together, and echos a new file, however that is irrelevant to the question. I just want to know how I can pass script2
as a parameter, i.e. the proper syntax.
如果这不可能,那么关于我如何规避该问题的任何提示都将是适当的.
If this is not possible, any hint as to how I may circumvent the issue would be appropriate.
推荐答案
如果您要将 script2 作为参数传递给 script1 ,以便在最后一个脚本中执行它,只需将以下代码放入 script1 并按如下所示调用 script1 :
If you want to pass script2 as an argument to script1 to execute it inside the last one, just put the following code inside script1 and call script1 like this:
./script1 "./script2 file1 file2" file3 # file4 file5
脚本1 中的代码:
$1 # here you're executing ./script2 file1 file2
shift
another_command "$@" # do anything else with the rest of params (file3)
或者,如果您知道 script2 的参数数量并且它是固定的,也可以按照以下步骤进行操作:
Or if you know the number of params to script2 and it is fixed, you can also do it as follows:
./script1 ./script2 file1 file2 file3 # file4 file5
脚本1 中的代码:
"$1" "$2" "$3"
shift 3
another_command "$@" # do anything else with the rest of params (file3)
这篇关于bash-将脚本作为另一个脚本的参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!