如何使用shell脚本(和makefile?)执行python程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32814489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to execute python program using a shell script (and makefile?)
提问by e04
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.
我已经阅读了许多教程和 stackex 问题/答案,但显然我的问题太具体了,而且我的知识太有限,无法拼凑出一个解决方案。
[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!
[编辑]我的困惑主要是因为我的项目需要一个 shell 脚本和一个 makefile 来运行一个简单的 python 程序。我不知道为什么有必要这样做,因为这似乎是一种迂回的做事方式。看起来 makefile 和脚本可能只是为了让自动分级器高兴,正如下面的受访者所提到的。所以,我想这是最好的与教授澄清的事情,然后。我真的很感激答案——非常感谢你的帮助!
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.py
通过program.sh
(shell 脚本)运行(我的源代码),这样当我在命令行中键入以下内容时
./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
它运行program.py
并传递arg1
到程序中,就像我在命令行中手动输入了以下内容一样
$ 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.
我还需要自动设置program.sh
为可执行文件,因此$ chmod +x program.sh
不必事先输入。
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.
我已经阅读了此处提供的解决方案,这非常有帮助,但这似乎需要使用.py
扩展名执行文件,而我的特定应用程序需要.sh
扩展名,以便按上述要求运行。
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?).
关于为什么我想运行一个.sh
文件的另一个原因是因为我还需要以某种方式包含一个 makefile 来运行程序以及脚本(所以我假设我必须make
在脚本中包含一个命令? )。
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:
老实说,我不知道为什么 python 程序需要 makefile,但是教授告诉我们,makefile 对于评分脚本来说会更方便,我们应该简单地为 makefile 的内容编写以下内容:
all:
/bin/true
Thanks so much in advance for any help in this matter!
非常感谢您对此事的任何帮助!
采纳答案by thenaterhood
You could write your shell script to call your python script with:
你可以编写你的 shell 脚本来调用你的 python 脚本:
$ python your_program.py
if you only need to pass one argument to your python script, assuming there will not be spaces in that argument.
如果您只需要将一个参数传递给您的 Python 脚本,则假设该参数中没有空格。
If you need a makefile, you can have it make your shell script executable.
如果你需要一个 makefile,你可以让它使你的 shell 脚本可执行。
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:
考虑到 makefile 是为了方便评分脚本,我怀疑您是否需要在脚本中添加对它的调用,但我会检查一下。评分脚本的作用可能是:
$ make
$ ./your_script.sh
回答by Amadan
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):
您可以通过两种方式执行 Python 程序(对于任何其他解释性语言也是如此):
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
1)显式运行解释器:python program1.py arg1
,它将找到默认的Python解释器(通常/usr/bin/python
)并告诉它运行你的脚本
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.
2)在脚本前加上“hashbang”魔法注释,并使脚本可执行:如果你放在第一行#!/usr/bin/python
并执行chmod a+x program1.py
(使program1.py
每个人都可以执行),那么每次输入时./program.py arg1
,就像你跑了一样/usr/bin/python program1.py args1
。正如我之前所说,你只需要做chmod
一次。
You can use both styles in both shell scripts and Makefiles equally; there is no need for a separate sh
file.
您可以在 shell 脚本和 Makefile 中同等地使用这两种样式;不需要单独的sh
文件。
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.
此外,您被指示拥有的 Makefile 没有任何意义,因为它什么也不做。可能是你的教授打算用他自己的来代替它,或者可能有其他解释,但all: /bin/true
本身没有任何意义。
If you really want to have a shell script that will pass all its arguments to a python script, here it is:
如果你真的想要一个 shell 脚本来将它的所有参数传递给一个 python 脚本,这里是:
#!/bin/sh
python program1.py "$@"