为什么这个 shell 脚本将自己称为 python 脚本?
显然这个 shell 脚本将自己称为 Python 脚本:(
#!/bin/sh
## repo default configuration
##
REPO_URL='git://android.git.kernel.org/tools/repo.git'
REPO_REV='stable'
magic='--calling-python-from-/bin/sh--'
"""exec" python -E "$0" "$@" """#$magic"
if __name__ == '__main__':
import sys
if sys.argv[-1] == '#%s' % magic:
del sys.argv[-1]
del magic
:
:
整个脚本: https://android.googlesource.com/tools/repo/+/v1.0/repo)
任何人都可以解释
这样调用它的目的吗?
为什么不在第一行添加#!/usr/bin/env python
以便对其进行解释 从一开始就作为Python脚本?添加那个神奇的最后一个命令行参数(后来在Python代码的开头被删除)的目的是什么?
Obviously this shell script is calling itself as a Python script:
#!/bin/sh
## repo default configuration
##
REPO_URL='git://android.git.kernel.org/tools/repo.git'
REPO_REV='stable'
magic='--calling-python-from-/bin/sh--'
"""exec" python -E "$0" "$@" """#$magic"
if __name__ == '__main__':
import sys
if sys.argv[-1] == '#%s' % magic:
del sys.argv[-1]
del magic
:
:
(Whole script: https://android.googlesource.com/tools/repo/+/v1.0/repo)
Can anyone explain
the purpose of calling it this way?
Why not having#!/usr/bin/env python
in the first line so it gets interpreted
as Python script from the beginning?the purpose of adding that magic last command line argument, that is removed afterwards in the beginning of the Python code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的第一个问题:这样做是为了修复不处理 #! 的 UNIX 系统(或其模拟系统)。正确或完全正确。高雅的艺术是制作一个在 shell 和其他语言中都正确的脚本。对于 perl,人们经常会看到这样的情况:
exec 由 shell 解释并执行,但是 perl 解释器看到一个条件语句 (.... if ...) 并且不执行任何操作,因为条件为 false。
Your first question: this is done to fix unix systems (or emulations thereof) that do not handle the #! correctly or at all. The high art is to make a script that is correct in shell as well as in the other language. For perl, one often sees something like:
The exec is interpreted and executed by the shell, but the perl interpreter sees a conditional statement (.... if ...) and does nothing because the condition is false.