简体   繁体   English

为什么OSX要求shebang解释器为二进制文件?

[英]Why does OSX require shebang interpreters to be binaries?

Referencing the answer to this question, When writing a haskell script, get syntax error near unexpected token . 引用此问题的答案, 在编写haskell脚本时,在意外标记附近获取语法错误

It turns out that when writing a script and telling bash on Mac OS X Mavericks to run it with a certain interpreter like so: #!/usr/local/bin/myinterpreter will fail if myinterpreter is also a shell script. 事实证明,当编写脚本并告诉bash在Mac OS X Mavericks上使用某种解释器运行时,如下所示: #!/usr/local/bin/myinterpreter如果myinterpreter器也是shell脚本,则将失败。 However this works fine on Debian and ArchLinux. 但是,这在Debian和ArchLinux上运行良好。 What is the difference in implementations that causes this to happen, and why did Apple implement it as such? 导致这种情况发生的实现方式有什么区别?为什么苹果要照此实现? Was there a tradeoff made here, or is this just a bug? 是在这里进行权衡还是仅仅是个错误?

See this page for all sorts of information about shebang lines. 有关shebang行的各种信息,请参见此页面

For this question this section is the relevant one: 对于这个问题, 本节是相关的:

interpreter itself as #! script
or: can you nest #!?

Most probably there isn't any Bell-Labs- or Berkeley-derived Unix that accepts the interpreter to be a script, which starts with #! again. 
However, Linux since 2.6.27.9[2] and Minix accept this.

Be careful not to confuse whether the kernel accepts it, or if the kernel has returned with an ENOEXEC 
and your shell silently tries to take over, parsing the #! line itself.

- bash-1 behaves so (the line length then is truncated to 80 characters and argv[0] becomes the invoked script.)
- bash-2, -3 and -4 do so, if the #! mechanism was not present at compile time (probably only in unix-like environments like cygwin).
- The original Almquist shell also recognizes #!, but only if "BSD" was not defined at compile time. Later variants de-facto do not recognize it.
- If a filesystem is mounted with a "noexec" option, the shell might take over as well (pointed out by Carl Lowenstein).

[2] For more information about nested #! on Linux, see the kernel patch (applied to 2.6.27.9) and especially binfmt_script.c which contains the important parts. 
Linux allows at most BINPRM_MAX_RECURSION, that is 4, levels of nesting. 
(hint to me about the change by Mantas Mikulėnas.)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM