views:

126

answers:

2

Consider this setup and folder structure:

c:\foo
      \bin\foo.bat
      \lib\foo.py

I have foo.bat path added to my environment PATH, so I can call it from anywhere passing in some arguments:

c:/>foo.bat -v 

foo.bat contains this code:

@ECHO OFF
"c:\foo\lib\foo.py" %1 %2 %3 %4 %5 %6 %7 %8 %9

This works fine in Windows.

Now I want to be able to do the same in Mac or Linux.

How can I create this executable file, that will call the script lib\foo.py, passing in some arguments?

thanks

[SOLUTION]

thanks guys, your answers helped me end up with this script that works as intended:

in \foo\bin\foo file

#!/usr/bin/env bash
python /usr/local/foo/lib/foo.py $*
+1  A: 

You didn't explicitly say what your shell of choice is, but assuming it's GNU bash:

#!/usr/bin/env bash
/path/to/lib/foo.py $*

Note that both your shell script and foo.py need to have the execution bit set to be run this way (that is only a

chmod +x /path/to/your/script /path/to/lib/foo.py
away).

Emanuele Cipolla
thanks Emanuele. That really helped as I wasn't sure how to pass in those arguments.
Bach
+4  A: 

You can call python scripts directly on mac/linux, just make sure to put your python interpreter on the first line, example file foo:

#!/usr/bin/python 
if __name__ == "__main__":
    print 'bar'

to run the file call it directly form your current directory using ./foo

if you want to access if from everywhere you can put it in /usr/local/bin, for example, just make sure that the file is executable (chmod +x foo).

deif
thank deif. The only problem is I didn't want the user to call in the script directly, as I was keeping all executables in the bin, while the script lives in the libs where it's got relative references to other libraries/scripts in that folder.
Bach