Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I install a script to run anywhere from the command line?

If I have a basic Python script, with it's hashbang and what-not in place, so that from the terminal on Linux I can run

/path/to/file/MyScript [args] 

without executing through the interpreter or any file extensions, and it will execute the program.

So would I install this script so that I can type simply

MyScript [args] 

anywhere in the system and it will run? Can this be implemented for all users on the system, or must it be redone for each one? Do I simply place the script in a specific directory, or are other things necessary?

like image 890
jeffbr13 Avatar asked Aug 06 '11 14:08

jeffbr13


2 Answers

The best place to put things like this is /usr/local/bin.

This is the normal place to put custom installed binaries, and should be early in your PATH.

Simply copy the script there (probably using sudo), and it should work for any user.

like image 60
agf Avatar answered Oct 01 '22 18:10

agf


Walkthrough of making a python script available anywhere:

Make a python script:

cd /home/el/bin touch stuff.py chmod +x stuff.py 

Find out where your python is:

which python /usr/bin/python 

Put this code in there:

#!/usr/bin/python print "hi" 

Run in it the same directory:

python stuff.py 

Go up a directory and it's not available:

cd .. stuff.py  -bash: stuff.py: command not found 

Not found! It's as we expect, add the file path of the python file to the $PATH

vi ~/.bashrc 

Add the file:

export PATH=$PATH:/home/el/bin 

Save it out, re apply the .bashrc, and retry

source ~/.bashrc 

Try again:

cd /home/el stuff.py 

Prints:

hi 

The trick is that the bash shell knows the language of the file via the shebang.

like image 23
Eric Leschinski Avatar answered Oct 01 '22 16:10

Eric Leschinski