简体   繁体   English

从外壳运行python脚本

[英]Running a python script from shell

I want to run a python script from a shell script. 我想从外壳程序脚本运行python脚本。 I have been able to run it from the "command window" (Cygwin), but now I want to be able to call it from a shell script. 我已经能够从“命令窗口”(Cygwin)运行它,但是现在我希望能够从shell脚本中调用它。 It does not work the way I thought it would and the Internet has not really helped me with it. 它无法按我认为的方式工作,并且Internet并没有真正帮助我。

My code so far: 到目前为止,我的代码:

#!/bin/bash
python Calibrate.py

I hope someone could help me further. 我希望有人能进一步帮助我。 Calibrate just contains a few basic command lines to transform some data that has been read in with an excelreader in the script itself. Calibrate仅包含一些基本命令行,以转换脚本本身中已使用excelreader读取的某些数据。

Just make sure the python executable is in your PATH environment variable, then add in your script 只要确保python可执行文件在PATH环境变量中,然后在脚本中添加

In the file job.sh, 在文件job.sh中,

#!/bin/sh python <path/to/python/script>/python_script.py

Execute this command to make the script runnable for you : chmod u+x job.sh Run it : ./job.sh 执行以下命令以使脚本可运行: chmod u+x job.sh运行它: ./job.sh

A Bash script needs to use the Unix EOL (End Of Line) convention, which terminates each line with \\n . Bash脚本需要使用Unix EOL(行尾)约定,该约定以\\n结束每一行。 But it looks like your test.sh shell script file uses the Windows / DOS EOL convention, which terminates each line with \\r\\n . 但是,看来您的test.sh Shell脚本文件使用Windows / DOS EOL约定,该约定以\\r\\n结尾。

You need to get rid of those \\r characters in test.sh, and you need to set your editor to use Unix line endings. 您需要除去test.sh中的\\r字符,并且需要将编辑器设置为使用Unix行结尾。

A simple way to strip \\r out of a file is to use the Unix sed command; 从文件中剥离\\r一种简单方法是使用Unix sed命令。 I assume it's available in Cygwin: 我认为它可以在Cygwin中使用:

sed -i 's/\r//g' test.sh

This removes all \\r characters in test.sh, writing the result back to test.sh 这将删除test.sh中的所有\\r字符,并将结果写回到test.sh

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM