我正在编写一个bash脚本,它接受一些命令行参数(可能包含空格),并通过登录shell将它们全部传递给一个程序(/bin/some_program)。从bash脚本调用的登录shell将取决于用户的登录shell。在这个例子中,假设用户使用/bin/bash作为他们的登录shell...但它也可能是/bin/tcsh或其他任何东西。
如果我知道要传递给some_program的参数数量,我可以在我的bash脚本中放入以下几行:
然后按照以下方式调用上述脚本:
如果我知道要传递给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 -c '"$@"' _ "$cmd" "$@"
虽然如果不需要登录 shell,那么也无需调用新的 bash 实例,只需在当前脚本中运行即可。"$cmd" "$@"
- geirha"$cmd" "$@"
是因为该脚本是从一个需要访问用户环境的OSX程序中调用的。因此,在某些情况下,我将不得不调用/bin/tcsh、/bin/ksh等。 - JCOidl$cmd
)替换为下划线,结果也会出现“找不到命令”的错误。PS-感谢您在这里的来回交流。这非常有用。 - JCOidl