Calling a python function with options from shell script

1k Views Asked by At

I have a python script which takes various options from the command line

e.g.

-runs with gui

python myscript.py -gui

-runs without requiring user prompts

python myscript.py -aut

-runs with input data taken from input.py

python myscript.py input.py

-and combinations of the above e.g. runs automatically taking input from input.py

python myscript.py -aut input.py

I would like to be able to call this from anywhere on my linux box. How can I do this? I have tried aliasing this in my .bashrc file, but using this method it is unable to accept any of the input options. I have also tried

export PATH=$PATH:/path/to/folder/above_myscript/

but this only recognises the command if I type myscript, not python myscript . Typing just myscript obviously is unable to run the python script.

I have also tried writing a shell script which I could call.

#!/bin/bash
#file: myscript.sh

python '/path/to/folder/above_myscript/myscript.py'

However, again I am also unable to pass any options to it. What is the best solution to this problem?

2

There are 2 best solutions below

4
On BEST ANSWER

If you add #!/usr/bin/env python to the top of your myscript.py file and run chmod +x myscript.py on it you should be able to run myscript.py without needing to put python before it. This should let you use the PATH changing method you had before.

0
On

try this :

python /path/to/folder/above_myscript/myscript.py

or this :

cd /path/to/folder/above_myscript && python myscript.py

on your shell script.