I have a python program in that I wrote some python code (disk.py). I executed this program using the command
python disk.py in a terminal, it worked.
Now I want to execute it using a shell script.
To be able to execute as
./disk.py you need two things:
chmod +x disk.py
Shell script should be like:
#!/bin/sh python disk.py
As @SHW mentioned, the question actually asks about executing a python program in a shell script (not running the python script directly with
So, expanding on @SHW answer, your shell script should be like:
#!/bin/bash /usr/bin/python /absolute/path/to/your/disk.py
/usr/bin/python instead of just
python; using absolute paths helps the script to know exactly what python to use (to find the absolute path to your installed python use
Same story when using absolute paths with your python script instead of just
disk.py. In my case, I was trying to run a Django app from my bash script, so I had to add the absolute path of my
manage.py for it to run correctly.
Also, regarding the header of your bash script, there are several alternatives to
#!/bin/bash. Please, take a look at this question to know more about it.