简体   繁体   中英

Why embed repo command in a bash script?

I was investigating repo (from Android project) source code. 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. So there is my question, why do not directly use 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 :

We need to pass the -E flag, but env on some platforms wasn't taking it. 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".

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:

#!/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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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