Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute python program using a shell script (and makefile?)

I've read numerous tutorials and stackex questions/answers, but apparently my questions is too specific and my knowledge too limited to piece together a solution.

[Edit] My confusion was mostly due to the fact that my project required both a shell script and a makefile to run a simple python program. I was not sure why that was necessary, as it seemed like such a roundabout way to do things. It looks like the makefile and script are likely just there to make the autograder happy, as the kind respondents below have mentioned. So, I guess this is something best clarified with the prof, then. I really appreciate the answers--thanks very much for the help!

Basically, what I want to do is to run program.py (my source code) via program.sh (shell script), so that when I type the following into the command line

./program.sh arg1

it runs program.py and passes arg1 into the program as though I had manually typed the following into the command line

$ python program.py arg1

I also need to automatically set program.sh to executable, so that $ chmod +x program.sh doesn't have to be typed beforehand.

I've read the solution presented here, which was very helpful, but this seems to require that the file be executed with the .py extension, whereas my particular application requires a .sh extension, so as to be run as desired above.

Another reason as to why I'd like to run a .sh file is because I also need to somehow include a makefile to run the program, along with the script (so I'm assuming I'd have to include a make command in the script?).

Honestly, I am not sure why the makefile is necessary for python programs, but we were instructed by the prof that the makefile would simply be more convenient for the grading scripts, and that we should simply write the following for the makefile's contents:

all:
   /bin/true

Thanks so much in advance for any help in this matter!

like image 935
e04 Avatar asked Sep 28 '15 01:09

e04


People also ask

How do I run a python script from a shell script?

Create a new shell file job. So let's say: touch job.sh and add command to run python script (you can even add command line arguments to that python, I usually predefine my command line arguments). I use this usually when I have to run multiple python files with different arguments, pre defined.

How do you execute a Python program from terminal?

But really large Python programs with a lot of complexity are written in files with a .py extension, typically called Python scripts. Then you execute them from the terminal using the Python command. All the commands we executed previously via the shell, we can also write it in a script and run in this way.

What is the use of Makefile in Python?

It's very rarely used in Python projects like this, because the difference in performance is in most cases negligible. More often than not, Makefiles are used to set up a project, clean it up, maybe provide some help and test your modules. The following is an example of a much more realistic Python project Makefile:

What happens if you can't execute or run a python script?

If you can't execute or run a Python script, then programming is pointless. When you run a Python script, the interpreter converts a Python program into something that that the computer can understand. Executing a Python program can be done in two ways: calling the Python interpreter with a shebang line, and using the interactive Python shell.


2 Answers

You could write your shell script to call your python script with:

$ python your_program.py $1

if you only need to pass one argument to your python script, assuming there will not be spaces in that argument.

If you need a makefile, you can have it make your shell script executable.

Considering that the makefile is for the convenience of the grading scripts, I doubt you need to add a call to it to your script, but I would check on that. Likely what the grading script does is:

$ make
$ ./your_script.sh 
like image 178
thenaterhood Avatar answered Sep 22 '22 18:09

thenaterhood


You can't set something to executable without running something; and since once is enough, you might as well just do it manually. (That's what all of us are doing with any files that are not autogenerated.)

You can execute a Python program in two ways (and the same goes for any other interpreted language):

1) Run the interpreter explicitly: python program1.py arg1, which will find the default Python interpreter (typically /usr/bin/python) and tell it to run your script

2) Prefix the script with the "hashbang" magic comment, and make the script executable: if you put in the first line #!/usr/bin/python and execute chmod a+x program1.py (make program1.py executable for everybody), then every time you type ./program.py arg1, it will be as if you ran /usr/bin/python program1.py args1. As I said before, you only need to do the chmod once.

You can use both styles in both shell scripts and Makefiles equally; there is no need for a separate sh file.


Also, the Makefile you are instructed to have makes no sense, as it does nothing. It may be that your professor intends to replace it with his own, or there might be some other explanation, but all: /bin/true makes no sense on its own.


If you really want to have a shell script that will pass all its arguments to a python script, here it is:

#!/bin/sh

python program1.py "$@"
like image 44
Amadan Avatar answered Sep 23 '22 18:09

Amadan