gpt4 book ai didi

bash - #! 脚本可以用作解释器吗?散列线?

转载 作者:行者123 更新时间:2023-11-29 08:51:14 25 4
gpt4 key购买 nike

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

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

/bin/interpreter:[归根所有;可执行]

#!/bin/bash

echo "I am an interpreter running " $1

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

#!/bin/interpreter

Here are some commands for the custom interpreter.

根据我对 hashbang 机制的了解,该脚本应按如下方式执行:

$ ./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 上做的)。

最佳答案

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

#!/bin/bash /bin/interpreter

Here are some commands for the custom interpreter.

bash 将按预期使用 $1 中的脚本路径运行您的解释器。

关于bash - #! 脚本可以用作解释器吗?散列线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6637135/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com