Just Jack
Just Jack

Reputation: 47

Calling script-level Python functions from the Linux command line

How can I create functions and software for my Linux server? Let me explain this in a bit more detail.

So for my Linux server which I access with my SSH client, I have made a few Python scripts that work fine, but what I really want to do is have these Python scripts active all the time, such that I can execute functions I've created in the script (such as "def time(): ...") just by typing "time" in to the command line rather than starting up a script with ./script-name.py and then type "time". Do I need to install my Python files in to the system in some way?

I struggled searching Google because I didn't fully understand what to search, and results that came up weren't really related to my request. I did find the cmd Python module and learned how to create cmd interpreters, however, in order for me to access the commands I defined in the cmd interpreter, I had to first start the script, which as I explained above, not what I am looking for.

How can I make script-level Python functions callable from the Linux command line?

Upvotes: 3

Views: 1266

Answers (2)

z0r
z0r

Reputation: 8595

If you're using Python, you'll still need to fire up the interpreter, but you can make that happen automatically.

Start by making your script executable. Run this command in the shell:

chmod +x script-name.py
ls -l script-name.py

The output of ls should look something like this (note the xs in the left-hand column):

-rwxr-xr-x  1 me me     4 Jan 14 11:02 script-name.py

Now add an interpreter directive line at the top of your script file - this tells the shell to run Python to interpret your script:

#!/usr/bin/python

Then add code at the end of the file that calls your function:

if __name__ == '__main__':
    time()

The if statement checks to see if this is the file that is being executed. It means you can still import your module from another Python file without the time() function being automatically called, if you want.

Finally, you need to put your script in the executable path.

mkdir -p $HOME/bin
mv script-name.py $HOME/bin/
export PATH=$HOME/bin:$PATH

Now you should be able to run script-name.py, and you'll see the output of the time() function. You can rename your file to whatever you like; you can even remove the .py extension.

Additional things you can do:

  • Use the argparse module to add command line arguments, e.g. so you can run script-name.py time to execute the time() function
  • Put the script in a system-wide path, like /usr/local/bin, or
  • Add the export PATH=$HOME/bin:$PATH line to your .bashrc so that it happens by default when you log in

Upvotes: 7

Jeff Sell
Jeff Sell

Reputation: 81

The answer above is by far more complete and more informative than mine. I just wanted to offer a quick and dirty alternative.

echo "alias time='<your script> time'" > ~/.bashrc
bash

Like I said, quick and dirty.

Upvotes: 2

Related Questions