脚本可以被 #! 用作解释器吗?哈希邦线?

发布于 2024-11-19 04:05:24 字数 921 浏览 1 评论 0原文

我正在尝试编写一个 bash 脚本,它将充当基本解释器,但它似乎不起作用:自定义解释器似乎没有被调用。我做错了什么?

下面是一个说明问题的简单设置:

/bin/interpreter: [owned by root;可执行文件]

#!/bin/bash

echo "I am an interpreter running " $1

/Users/zeph/script 归我所有,并且可执行:

#!/bin/interpreter

Here are some commands for the custom interpreter.

根据我对 hashbangs 机制的了解,该脚本应该可执行如下:

$ ./script
I am an interpreter running ./script

但这不起作用。相反,会发生以下情况:

$ ./script 
./script: line 3: Here: command not found

...似乎 /bin/bash 正在尝试解释 ./script 的内容。我做错了什么?

注意:虽然看起来 /bin/interpreter 从未调用过,但如果它不存在,我确实会收到错误:(

$ ./script
-bash: ./script: /bin/interpreter: bad interpreter: No such file or directory

第二个注意点:如果有任何区别,我我正在 MacOS X 上执行此操作)。

I'm trying to write a bash script which will behave as a basic interpreter, but it doesn't seem to work: The custom interpreter doesn't appear to be invoked. What am I doing wrong?

Here's a simple setup illustrating the problem:

/bin/interpreter: [owned by root; executable]

#!/bin/bash

echo "I am an interpreter running " $1

/Users/zeph/script is owned by me, and is executable:

#!/bin/interpreter

Here are some commands for the custom interpreter.

From what I understand about the mechanics of hashbangs, the script should be executable as follows:

$ ./script
I am an interpreter running ./script

But this doesn't work. Instead the following happens:

$ ./script 
./script: line 3: Here: command not found

...It appears that /bin/bash is trying to interpret the contents of ./script. What am I doing wrong?

Note: Although it appears that /bin/interpreter never invoked, I do get an error if it doesn't exist:

$ ./script
-bash: ./script: /bin/interpreter: bad interpreter: No such file or directory

(Second note: If it makes any difference, I'm doing this on MacOS X).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

魂牵梦绕锁你心扉 2024-11-26 04:05:24

为了完成这项工作,您可以将解释器的解释器(即 bash)添加到 shebang:

#!/bin/bash /bin/interpreter

Here are some commands for the custom interpreter.

bash 然后将使用 $1 中的脚本路径运行您的解释器> 正如预期的那样。

To make this work you could add the interpreter's interpreter (i.e. bash) to the shebang:

#!/bin/bash /bin/interpreter

Here are some commands for the custom interpreter.

bash will then run your interpreter with the script path in $1 as expected.

月依秋水 2024-11-26 04:05:24

您不能直接使用脚本作为 #! 解释器,但您可以使用以下命令通过 env 命令间接运行脚本:

    #!/usr/bin/env /bin/interpreter

/usr/bin/env 本身就是一个二进制文件,因此是 #! 的有效解释器; /bin/interpreter 可以是您喜欢的任何内容(任何类型的脚本或二进制文件),而不必将其自己的解释器的知识放入调用脚本中。

You can't use a script directly as a #! interpreter, but you can run the script indirectly via the env command using:

    #!/usr/bin/env /bin/interpreter

/usr/bin/env is itself a binary, so is a valid interpreter for #!; and /bin/interpreter can be anything you like (a script of any variety, or binary) without having to put knowledge of its own interpreter into the calling script.

江湖正好 2024-11-26 04:05:24

阅读您系统的 execve 手册页。它规定了脚本如何启动,并且应该指定 hash-bang 行中的解释器是二进制可执行文件。

Read the execve man page for your system. It dictates how scripts are launched, and it should specify that the interpreter in a hash-bang line is a binary executable.

久夏青 2024-11-26 04:05:24

我在 comp.unix.shell 中提出了类似的问题这提出了一些相关信息。

同一主题的第二个分支进一步的想法。

最通用的 UNIX 解决方案是让 shebang 指向二进制可执行文件。但该可执行程序可以像对 execl() 的一次调用一样简单。两个线程都指向名为 gscmd 的程序的示例 C 源代码,该程序只不过是 execv("gs",...) 的包装器。

I asked a similar question in comp.unix.shell that raised some pertinent information.

There was a second branch of the same thread that carried the idea further.

The most general unix solution is to have the shebang point to a binary executable. But that executable program could be as simple as a single call to execl(). Both threads lead to example C source for a program called gscmd, which is little more than a wrapper to execv("gs",...).

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