Question

This is for a small project proposal.
I want to create many user defined unix commands and perform various operations on files using codes written in Python.
E.g. extractinfo 'filename' [This command would extract the metadata or general information stored in some file]

This is how I am going around:
1. Suppose I have a python code extract.py.
2. Now, in order to create 'my own unix command' (say - 'extractinfo') , I write a shell script named - extractinfo:

#/bin/bash
python /home/extract.py  

3. Now, I want to place the above shell script in /usr/bin. So I create another shell script - install.sh:

#!/bin/bash
chmod u+x extractinfo  
sudo cp extractinfo /usr/bin/  

4. Now I can just run install.sh and use the command 'extractinfo'.

The problem is, if I use another system and run install.sh, the command 'extractinfo' becomes available but then on executing the command on terminal, an error (python: can't open file 'extract.py': [Errno 2] No such file or directory) is thrown.

How to take care of the path of the original python script when one needs to use another system?

I know trying to link shell and python is very crude way of doing things. Any help/suggestions, in regards to this problem, are appreciated.

Was it helpful?

Solution

Is there some reason not to just use shebang for python directly? Possibly with /usr/bin/env indirection if you're concerned about the path to python not being consistent?

Refer to http://en.wikipedia.org/wiki/Shebang_%28Unix%29 if you're not familiar. It's not clear to me that you need a bash script at all. Just put the python script in /usr/bin (or probably better, /usr/local/bin)

In other words, if that wasn't quite clear, make the first line of your script either: #!/usr/bin/python or #!/usr/bin/env python

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top