简体   繁体   English

为什么在bash脚本中嵌入repo命令?

[英]Why embed repo command in a bash script?

I was investigating repo (from Android project) source code. 我正在调查repo(来自Android项目)的源代码。 It start with the following : 它从以下开始:

#!/bin/sh
magic='--calling-python-from-/bin/sh--'
"""exec" python -E "$0" "$@" """#$magic"

If I understand it well, it means that the script is recalling itself with python. 如果我理解得很好,那就意味着脚本正在用python回忆起来。 So there is my question, why do not directly use python. 所以有我的问题,为什么不直接使用python。

For example I usually use something like : 例如,我通常使用类似的东西:

#!/usr/bin/env python

I think there is a valuable reason, but I can't figure it out. 我认为有一个很有价值的理由,但我无法弄清楚。

Thanks 谢谢

Google developer Shawn Pearce gives the reason in this discussion : Google开发人员Shawn Pearce在本次讨论中说明了原因:

We need to pass the -E flag, but env on some platforms wasn't taking it. 我们需要传递-E标志,但某些平台上的env没有接受它。 So I cooked up this work around. 所以我把这项工作搞定了。 It mostly had to do with our internal desktops at Google; 它主要与谷歌的内部桌面有关; they have a lot of PYTHON environment flags that we didn't want to inherit into the repo process (because they are there for normal Google engineers, not Android Google engineers), and at least at the time env on either Mac OS or Linux (I can't remember which) was rejecting a shbang line of "#!/usr/bin/env python -E". 他们有很多PYTHON环境标志我们不想继承到repo过程(因为它们适用于普通的谷歌工程师,而不是Android谷歌的工程师),至少在Mac OS或Linux上是环境的(我不记得是哪一个拒绝了“#!/ usr / bin / env python -E”的shbang系列。

Perl and Ruby have a '-x' command-line switch, used to do something like setting up shell environment variables before starting the interpeter itself -- mixing shell commands and perl/ruby in the same file: Perl和Ruby有一个'-x'命令行开关,用于在启动interpeter之前设置shell环境变量 - 在同一个文件中混合shell命令和perl / ruby​​:

#!/bin/sh
export PERL5LIB=/some/perl/lib/path:$PERL5LIB
export FOO=1

exec perl -x $0 $@

# ^^^^ ---- shell commands above this line ---- ^^^^
#!perl
# vvvv ---- perl script below this line ---- vvvv
use strict;

print "Hello world\n":

The 'magic' bit in repo is the author's solution to this problem -- but less flexible and far more obtuse. 回购中的“神奇”位是作者对这个问题的解决方案 - 但不那么灵活,而且更加迟钝。 It's sadly a missing feature in python. 令人遗憾的是,python中缺少一个功能。

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

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