How to manage python shebang on mixed systems?

2QdxY4RzWzUUiLuE at potatochowder.com 2QdxY4RzWzUUiLuE at potatochowder.com
Mon Nov 7 07:44:21 EST 2022


On 2022-11-07 at 11:56:19 +0000,
"Weatherby,Gerard" <gweatherby at uchc.edu> wrote:

> Write the wrapper script.
> 
> #!/bin/bash
> if [ $(hostname) == "oldystem" ]; then
>     exec /usr/bin/python $*
> else
>     exec /usr/bin/python2 $*
> fi

Use "$@" (with the quotes) instead of a bare $*.  The former preserves
quoted arguments, while the latter does not.

Also, it's probably more robust to check explcitly for python2 instead
of depending on the hostname.  Renaming hosts isn't a common operation,
but in general, checking for what something *does* or *has* is better
than checking for what it *is* (hey, look:  duck typing!).

Implementing both suggestions yields this:

#! /bin/bash
if [ -x /usr/bin/python2 ]; then
    exec /usr/bin/python2 "$@"
else
    exec /usr/bin/python "$@"
fi

YMMV.


More information about the Python-list mailing list