在启动可执行文件之前在 bash 中获取脚本文件
我正在尝试编写一个 bash 脚本,该脚本“包装”用户想要调用的任何内容(及其参数),在实际调用它之前获取固定文件。
澄清一下:我有一个“ConfigureMyEnvironment.bash”脚本,必须在启动某些可执行文件之前获取该脚本,因此我希望有一个“LaunchInMyEnvironment.bash”脚本,您可以使用它,如下所示:
LaunchInMyEnvironment <whatever_executable_i_want_to_wrap> arg0 arg1 arg2
我尝试了以下 LaunchInMyEnvironment.bash:
#!/usr/bin/bash
launchee="$@"
if [ -e ConfigureMyEnvironment.bash ];
then source ConfigureMyEnvironment.bash;
fi
exec "$launchee"
我必须使用“launchee”变量来保存 $@ var,因为执行源代码后,$@ 变空。
无论如何,这不起作用并且失败如下:
myhost $ LaunchInMyEnvironment my_executable -h
myhost $ /home/me/LaunchInMyEnvironment.bash: line 7: /home/bin/my_executable -h: No such file or directory
myhost $ /home/me/LaunchInMyEnvironment.bash: line 7: exec: /home/bin/my_executable -h: cannot execute: No such file or directory
也就是说,似乎“-h”参数被视为可执行文件名的一部分而不是参数......但这实际上没有意义我。 我也尝试使用 $* 而不是 $@,但没有更好的结果。
我做错了什么?
安德里亚.
I'm trying to write a bash script that "wraps" whatever the user wants to invoke (and its parameters) sourcing a fixed file just before actually invoking it.
To clarify: I have a "ConfigureMyEnvironment.bash" script that must be sourced before starting certain executables, so I'd like to have a "LaunchInMyEnvironment.bash" script that you can use as in:
LaunchInMyEnvironment <whatever_executable_i_want_to_wrap> arg0 arg1 arg2
I tried the following LaunchInMyEnvironment.bash:
#!/usr/bin/bash
launchee="$@"
if [ -e ConfigureMyEnvironment.bash ];
then source ConfigureMyEnvironment.bash;
fi
exec "$launchee"
where I have to use the "launchee" variable to save the $@ var because after executing source, $@ becomes empty.
Anyway, this doesn't work and fails as follows:
myhost $ LaunchInMyEnvironment my_executable -h
myhost $ /home/me/LaunchInMyEnvironment.bash: line 7: /home/bin/my_executable -h: No such file or directory
myhost $ /home/me/LaunchInMyEnvironment.bash: line 7: exec: /home/bin/my_executable -h: cannot execute: No such file or directory
That is, it seems like the "-h" parameter is being seen as part of the executable filename and not as a parameter... But it doesn't really make sense to me.
I tried also to use $* instead of $@, but with no better outcoume.
What I'm doing wrong?
Andrea.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试划分你的参数列表:
然后尝试:
调试器
Try dividing your list of argumets:
And try then:
Debugger
正常执行即可,无需
exec
Just execute it normally without
exec
您可能想尝试一下(未经测试):
exec 的语法是
exec command [arguments]
,但是由于您引用了$launchee
,因此它被视为单个参数 - 即命令,而不是命令及其参数。另一种变体可能是简单地执行:exec $@
You might want to try this (untested):
The syntax for exec is
exec command [arguments]
, however becuase you've quoted$launchee
, this is treated as a single argument - i.e., the command, rather than a command and it's arguments. Another variation may be to simply do:exec $@
试试这个:
这将使用数组来存储参数,因此它将处理诸如“空格分隔字符串”和“内部带有 ; 的字符串”之类的调用
Upd:简单示例
test_array() { abc=("$@");对于“${abc[@]}”中的 x;执行 echo ">>$x<<";完毕; }
test_array "abc def" ghi
应该给出
Try this:
That will use arrays for storing arguments, so it will handle even calls like "space delimited string" and "string with ; inside"
Upd: simple example
test_array() { abc=("$@"); for x in "${abc[@]}"; do echo ">>$x<<"; done; }
test_array "abc def" ghi
should give
您是否尝试过删除 exec 命令中的双引号?
Have you tried to remove double quotes in exec command?