Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Macos 指向脚本的Shebang(也有Shebang)实际上被忽略了_Macos_Bash_Shell - Fatal编程技术网

Macos 指向脚本的Shebang(也有Shebang)实际上被忽略了

Macos 指向脚本的Shebang(也有Shebang)实际上被忽略了,macos,bash,shell,Macos,Bash,Shell,考虑以下代码: #!/usr/bin/env python import sys print "Hello! I've got %r as input." % sys.stdin.read() 这是/usr/local/bin/my_解释器中的chmod+xed脚本。这是: #!/usr/local/bin/my_interpreter This is intended to be passed "as is" to python script. 是试图利用它的chmod+xed脚本。

考虑以下代码:

#!/usr/bin/env python

import sys

print "Hello! I've got %r as input." % sys.stdin.read()
这是
/usr/local/bin/my_解释器中的
chmod+x
ed脚本。这是:

#!/usr/local/bin/my_interpreter

This is intended to be passed "as is" to python script.
是试图利用它的
chmod+x
ed脚本。如果我
回显某个|/usr/local/bin/my_解释器
,它可以正常工作,但一旦我尝试执行上面的脚本,它就会失败

/Users/modchan/test_interpreter/foo.bar: line 3: This: command not found

似乎
foo.bar
被悄悄地重定向到bash而不是我的脚本。我做错了什么?如何做到这一点?

这取决于您正在运行的操作系统的程序加载器,我从您的标记中将其视为OS X。许多类似UNIX的操作系统要求shebang解释器是已编译的可执行二进制文件,而不是带有另一个shebang的另一个脚本

)

Linux从2.6.27.9开始就支持这一点,但本文作者建议,可能没有任何Berkeley派生的Unixen(可能包括OS X)能够:

实现您想要的目标的一种方法如下:

$!/bin/sh
exec /usr/local/bin/my_interpreter <<EOM

... content to be executed ...
EOM
$!/usr/bin/env /usr/local/bin/my_interpreter
... content to be executed ...

看起来MacOSX要求解释器是二进制的,而不是另一个脚本。要使其工作,请将第二个脚本的解释器更改为

#!/usr/bin/env /usr/local/bin/my_interpreter
但是这里还有第二个问题:第二个脚本的内容将而不是转到其解释器的
stdin
,但是脚本路径名将作为命令行参数传递,即

/usr/bin/env /usr/local/bin/my_interpreter /Users/modchan/test_interpreter/foo.bar

你应该按文件名
sys.argv[1]
阅读该文件,而不是从
sys.stdin

与@moodywoody相比,imho问题不一样,但你链接的问题信息量很大。我不想说这个问题是重复的,只是另一个线程提供了信息。谢谢,在您回答之前,我已经尝试过使用
/usr/bin/env my_解释器
,但不知道文件名会转到argv而不是stdin。