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.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityShell script should be like:
#!/bin/sh
python disk.py
To be able to execute as ./disk.py
you need two things:
#!/usr/bin/env python
chmod +x disk.py
/usr/bin/python
, in others it might be /opt/bin/python
. With #!/usr/bin/env python
it should work in all systems where python is correctly installed, regardless of its exact location.
As @SHW mentioned, the question actually asks about executing a python program in a shell script (not running the python script directly with ./disk.py
)
So, expanding on @SHW answer, your shell script should be like:
#!/bin/bash
/usr/bin/python /absolute/path/to/your/disk.py
Notice the /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 which python
).
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.
Cheers.
./disk.py
(wild guess)? If so, you just need to add a suitable hash-bang, i.e.#!/usr/bin/python
. If you mean some else, you need to clarify what you are talking about.