【发布时间】:2012-08-21 16:49:36
【问题描述】:
我正在编写一个 bash 脚本,该脚本接受许多命令行参数(可能包括空格)并通过登录 shell 将所有这些参数传递给程序 (/bin/some_program)。从 bash 脚本调用的登录 shell 将取决于用户的登录 shell。假设用户在这个例子中使用 /bin/bash 作为他们的登录 shell……但它可能是 /bin/tcsh 或其他任何东西。
如果我知道将有多少个参数传递给 some_program,我可以在我的 bash 脚本中加入以下几行:
#!/bin/bash
# ... (some lines where we determine that the user's login shell is bash) ...
/bin/bash --login -c "/bin/some_program \"$1\" \"$2\""
然后调用上面的脚本如下:
my_script "this is too" cool
通过上面的例子,我可以确认 some_program 接收到两个参数,“this is too”和“cool”。
我的问题是……如果我不知道要传递多少个参数怎么办?我想将发送给 my_script 的所有参数传递给 some_program。问题是我无法弄清楚如何做到这一点。以下是一些行不通的事情:
/bin/bash --login -c "/bin/some_program $@" # --> 3 arguments: "this","is","too"
/bin/bash --login -c /bin/some_program "$@" # --> passes no arguments
【问题讨论】:
标签: bash