5

I have written a very simple command line utility for myself. The setup consists of:

  1. A single .py file containing the application/source.
  2. A single executable (chmod +x) shell script which runs the python script.
  3. A line in my .bash_profile which aliases my command like so: alias cmd='. shellscript' (So it runs in the same terminal context.)

So effectively I can type cmd to run it, and everything works great.

My question is, how can I distribute this to others? Obviously I could just write out these instructions with my code and be done with it, but is there a faster way? I've occasionally seen those one-liners that you paste into your console to install something. How would I do that? I seem to recall them involving curl and piping to sh but I can't remember.

4
  • 2
    Why do you need the shell script? Commented Mar 7, 2013 at 23:02
  • My script cds to different directories, but calling os.system('cd /my/path') doesn't work since that's a sub-shell (from what I understand). So basically my Python script writes the jump path to a temp file and the shell script reads it and does the change. Commented Mar 7, 2013 at 23:30
  • 1
    @hamstu: os.chdir() will change the current working directory of your running script. Commented Mar 8, 2013 at 5:23
  • os.chdir() won't quite get the job done, since it will change the directory in the subshell that the python script is running in, not in the shell the user is using. That's why sourcing the shell script is needed. Commented Dec 29, 2014 at 3:08

2 Answers 2

3

Upload your script to something like ideone. Then tell your friend to pipe it into python. Example script:

def print_message():
    print "This is my very special script!"

if __name__ == "__main__":
    print_message()

Example of running script:

username@server:~$ curl http://ideone.com/plain/O2n3Pg 2>/dev/null | python
This is my very special script!
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! Your example is just what I needed.
No probs! I should add a warning here that you should never do this unless you trust the script, as it could easily own your machine, much like running a random .exe in Windows...
1

chmod +x cmd.py

then they can type ./cmd.py

they can also use it piped.

I would add that unix users would probably already know how to make a file executable and run it, so all you'd have to do is make the file available to them.

Do make sure they know what version(s) of python they need to run your script.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.