在启动可执行文件之前在 bash 中获取脚本文件

发布于 2024-08-28 14:42:57 字数 1001 浏览 3 评论 0原文

我正在尝试编写一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

献世佛 2024-09-04 14:43:39

尝试划分你的参数列表:

ALL_ARG="${@}"
可执行文件=“${1}”
Rest_of_Args=${ALL_ARG##$可执行文件}

然后尝试:

$可执行文件$Rest_of_Args
(或执行 $Executable $Rest_of_Args)

调试器

Try dividing your list of argumets:

ALL_ARG="${@}"
Executable="${1}"
Rest_of_Args=${ALL_ARG##$Executable}

And try then:

$Executable $Rest_of_Args
(or exec $Executable $Rest_of_Args)

Debugger

拿命拼未来 2024-09-04 14:43:37

正常执行即可,无需exec

#!/usr/bin/bash
launchee="$@"
if [ -e ConfigureMyEnvironment.bash ];
     then source ConfigureMyEnvironment.bash;
fi

$launchee

Just execute it normally without exec

#!/usr/bin/bash
launchee="$@"
if [ -e ConfigureMyEnvironment.bash ];
     then source ConfigureMyEnvironment.bash;
fi

$launchee
是伱的 2024-09-04 14:43:34

您可能想尝试一下(未经测试):

#!/usr/bin/bash
launchee="$1"
shift
if [ -e ConfigureMyEnvironment.bash ];
     then source ConfigureMyEnvironment.bash;
fi

exec "$launchee" $@

exec 的语法是 exec command [arguments],但是由于您引用了 $launchee,因此它被视为单个参数 - 即命令,而不是命令及其参数。另一种变体可能是简单地执行:exec $@

You might want to try this (untested):

#!/usr/bin/bash
launchee="$1"
shift
if [ -e ConfigureMyEnvironment.bash ];
     then source ConfigureMyEnvironment.bash;
fi

exec "$launchee" $@

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 $@

疯到世界奔溃 2024-09-04 14:43:30

试试这个:

#!/usr/bin/bash
typeset -a launchee
launchee=("$@")
if [ -e ConfigureMyEnvironment.bash ]; 
  then source ConfigureMyEnvironment.bash; 
fi 
exec "${launchee[@]}"

这将使用数组来存储参数,因此它将处理诸如“空格分隔字符串”和“内部带有 ; 的字符串”之类的调用

Upd:简单示例

test_array() { abc=("$@");对于“${abc[@]}”中的 x;执行 echo ">>$x<<";完毕; }
test_array "abc def" ghi

应该给出

>>abc def<<
>>ghi<<

Try this:

#!/usr/bin/bash
typeset -a launchee
launchee=("$@")
if [ -e ConfigureMyEnvironment.bash ]; 
  then source ConfigureMyEnvironment.bash; 
fi 
exec "${launchee[@]}"

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

>>abc def<<
>>ghi<<
瑶笙 2024-09-04 14:43:25

您是否尝试过删除 exec 命令中的双引号?

Have you tried to remove double quotes in exec command?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文