我在SO上找不到类似的问题.
如何将bash脚本作为参数正确传递给另一个bash脚本.
例如,假设我有两个脚本可以接受许多参数,我想传递一个脚本作为另一个脚本的参数.就像是:
./script1 (./script2 file1 file2) file3
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,script2将file1和file2合并在一起,并回显一个新文件,但这与该问题无关.我只是想知道如何script2作为参数传递,即正确的语法.
如果这是不可能的,任何关于我如何规避问题的暗示都是合适的.
如果要将评估script2的结果作为参数传递,请使用$().请记住,你必须引用它.
./script1 "$(./script2 file1 file2)" file3
Run Code Online (Sandbox Code Playgroud)
如果要将script2作为参数传递给script1以在最后一个脚本中执行它,只需将以下代码放入script1中并像这样调用script1 :
./script1 "./script2 file1 file2" file3 # file4 file5
Run Code Online (Sandbox Code Playgroud)
script1内的代码:
$1 # here you're executing ./script2 file1 file2
shift
another_command "$@" # do anything else with the rest of params (file3)
Run Code Online (Sandbox Code Playgroud)
或者,如果您知道script2的参数数量并且它是固定的,您也可以按如下方式执行:
./script1 ./script2 file1 file2 file3 # file4 file5
Run Code Online (Sandbox Code Playgroud)
script1内的代码:
"$1" "$2" "$3"
shift 3
another_command "$@" # do anything else with the rest of params (file3)
Run Code Online (Sandbox Code Playgroud)