Reputation: 75
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!
Upvotes: 5
Views: 24575
Reputation: 198304
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 "$@"
Upvotes: 4
Reputation: 167
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
Upvotes: 4