bash-通脚本作为另一个脚本的参数 [英] bash- pass script as argument of another script

查看:161
本文介绍了bash-通脚本作为另一个脚本的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能于是找到了类似的问题。

I can't find a similar question on SO.

如何能够正确地传递一个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合并文件1和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 执行它的最后一个里面,只要把下面的code里面的 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

code SCRIPT1

$1 # here you're executing ./script2 file1 file2
shift
another_command "$@" # do anything else with the rest of params (file3)


或者,如果你知道PARAMS的要数 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

code SCRIPT1

"$1" "$2" "$3"
shift 3
another_command "$@" # do anything else with the rest of params (file3)

这篇关于bash-通脚本作为另一个脚本的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆