家当指向脚本(也有家当)被有效地忽略 [英] Shebang pointing to script (also having shebang) is effectively ignored
问题描述
考虑以下code:
#!/usr/bin/env python
import sys
print "Hello! I've got %r as input." % sys.stdin.read()
这是使用chmod + X
编辑在脚本在/ usr / local / bin目录/ my_inter preTER
。而这个:
#!/usr/local/bin/my_interpreter
This is intended to be passed "as is" to python script.
是使用chmod +,试图利用它X
ed脚本。如果我呼应的东西|在/ usr / local / bin目录/ my_inter preTER
,它工作正常,但一旦我尝试上述执行脚本,它失败
Is chmod +x
ed script that tries to make use of it. If I echo something | /usr/local/bin/my_interpreter
, it works fine, but once I try to execute script above, it fails with
/Users/modchan/test_interpreter/foo.bar: line 3: This: command not found
似乎 foo.bar
将被自动重定向到bash的,而不是我的脚本。我究竟做错了什么?如何使这项工作?
Seems that foo.bar
is silently redirected to bash instead of my script. What am I doing wrong? How to make this work?
推荐答案
看起来像Mac OS X的需要间preTER是二进制的,而不是另一个脚本。为了使它工作,更改第二个脚本的跨preTER为
Looks like Mac OS X requires interpreter to be binary, not another script. To make it work, change the second script's interpreter to
#!/usr/bin/env /usr/local/bin/my_interpreter
不过,你得在这里第二个问题:第二个脚本的内容将不可以转到标准输入
及其间$ P $的PTER,但脚本路径将作为命令行参数传递,即
But you've got a second problem here: the contents of the second script will not go to stdin
of its interpreter, but the script pathname will be passed as command line argument, i.e.
/usr/bin/env /usr/local/bin/my_interpreter /Users/modchan/test_interpreter/foo.bar
您必须阅读名称的文件 sys.argv中[1]
而不是从 sys.stdin
。
You shall read the file by name sys.argv[1]
rather than from sys.stdin
.
这篇关于家当指向脚本(也有家当)被有效地忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!