Shebang script interpreter from shell variable

I have several scripts that need to specify the python binary that runs them:

#! /home/nargle/python/bin/python2.6

      

I need to adapt these scripts to work on two different sites. Lots of tools are installed in different locations, so on a new site, 2 script needs to start with:

#! /user/nargle/python/bin/python2.6
..

      

I want to replace the direct link strings with environment variables that are set differently for each site. I would like this to work:

#! $MY_PYTHON_PATH

      

but it is not! I'm a little foggy where you can explore. It is an executing shell (be it bash, csh, or whatever) that detects '#!' at the beginning of the script (be it bash, python or whatever) and starts the interpreter / shell to run?

I feel like there must be some way to do this. Please advise!

Oh yes, there is another limitation: we cannot use a path for this . This may seem like a silly limitation, but it's for a large environment with many users.

RHEL 5.7 environment.

EDIT It was suggested to use a shell script and this is the current plan: it works great:

 $MY_PYTHON_PATH some_script file.py $@

      

The problem is that we have a lot of people using python files and a lot of automated tests that need to be changed. If it needs to be done, it needs to be done, but if possible, I want to minimize the impact of changing work practices on many people.

EDIT . It is also possible to have a link in the same place as on both systems and that links to real binary to a different target on each system. This is feasible, but it seems pretty messy: we use the linux module package to set environment variables for many tools, and it would be nice if we could take the python path from our modules.

EDIT This is not an answer, but it looks like what wicked hack I was looking for: http://docs.nscl.msu.edu/daq/bluebook/html/x3237.html .. see "Example 4-2. #! lines for bash and for tclsh "

EDIT I was hoping this might work, but it doesn't:

!# /usr/bin/env PATH=$PATH:$MY_PYTHON_PATH python2.6

      

+3


source to share


4 answers


A common solution is to change the shebang to

#!/usr/bin/env python2.6

      



Then just position your $ PATH to point to the correct python2.6 on each machine.

+5


source


Write a shell shell script. If you have script.py

, write script.py.sh

with the following content:

#!/bin/bash
PYTHON_SCRIPT=$( echo "$0" | sed -e 's/\.sh$//' )
exec $MY_PYTHON_PATH $PYTHON_SCRIPT "$@"

      

Disclaimer: This is untested, just written from my head.

Now just set up MY_PYTHON_PATH on each computer and call script.py.sh

instead script.py

.

Summary . This solution is only the second one because it requires a lot of script calls from script.py

to script.py.sh

, which should be avoided if at all possible.




Alternative

Use a env

python-finder script to call that simply calls the python binary contained in $MY_PYTHON_PATH

. The Python-finder script must be in the same place on both machines, use symbolic links if necessary.

#!/usr/bin/env /usr/local/bin/python-finder.sh

      

Python-finder.sh content:

#!/bin/bash
exec $MY_PYTHON_PATH "$@"

      

This works because for interpreter scripts (those that start with a shebang) execve calls the interpreter and passes the filename to env, which turns it into the command it calls.

+2


source


I was stupid: using an expansion variable with env actually works.

#! /usr/bin/env PATH="$PATH:$MY_PYTHON_PATH" python2.6

      

0


source


We can do it:

#!/bin/bash
"exec" "python" "$0"

print "Hello World"

      

from http://rosettacode.org/wiki/Multiline_shebang#Python

0


source







All Articles