简体   繁体   English

从python运行shell脚本

[英]Running shell script from python

I have written a small python programme which writes a C-shell script, and then calls the script with subprocess. 我编写了一个小的python程序,该程序编写了C-shell脚本,然后使用子进程调用该脚本。 However, python fails to execute the script. 但是,python无法执行脚本。 I tested this in the terminal and discovered that the script will also not run from the terminal (ie not a python issue). 我在终端中对此进行了测试,发现脚本也不会从终端运行(即不是python问题)。 Further testing revealed I could write an identical script in a text editor (vi) and then successfully execute it from the terminal (ie I don't think the contents of the script are at fault). 进一步的测试表明,我可以在文本编辑器(vi)中编写相同的脚本,然后从终端成功执行该脚本(即,我认为脚本的内容不存在问题)。 Eventually I found that if I tried to run the output from python directly in the terminal with ./myscript.com it would not run. 最终,我发现如果尝试使用./myscript.com直接在终端中运行python的输出,它将无法运行。 However, if I open with vi, make NO changes, save and then run from the terminal using ./myscript.com (ie the exact same command) it will execute. 但是,如果我用vi打开,进行任何更改,保存,然后使用从终端运行./myscript.com (即相同的指令),它会执行。 The script is included below, in case it is of use, however, most of the contents are very specific to the particular programme I am trying to run. 该脚本包含在下面,以备使用,但是,大多数内容都非常特定于我要运行的特定程序。 I have no idea how to debug this further, or why the effect of opening, saving and closing in vi is sufficient to allow the script to be executed. 我不知道如何进一步调试它,或者不知道为什么在vi中打开,保存和关闭的效果足以允许执行脚本。 (I have checked the permissions for example and these are unchanged after the vi step.) Any suggestions on how to proceed which be much appreciated. (例如,我已经检查了权限,并且在vi步骤之后这些权限没有更改。)有关如何进行的任何建议都值得赞赏。

#!/bin/csh
#
bin2pipe -in /full/path/to/this/script/recon.spc -bad 0.0 -noswap \
-xN 1714 \
-xT 1714 \
-xFT Freq \
-xMODE Real \
-xSW 4184.570312 \
-xCAR 10.929523 \
-xOBS 800.130005 \
-xLAB 1H \
-yN 2048 \
-yT 2048 \
-yFT Freq \
-yMODE Real \
-ySW 1700.680054 \
-yCAR 125.728023 \
-yOBS 81.075996 \
-yLAB 1H \
-ndim 2 \
|nmrPipe -fn TP -auto \
|nmrPipe -fn REV \
|nmrPipe -fn TP -auto \
-out recon.spc.pipe.ft2 -ov -verb

In case this is useful I used xxd (as suggested in the comments above, http://linuxcommand.org/man_pages/xxd1.html ) to convert both script files, one created in python and one written manually, to hex 如果这很有用,我可以使用xxd (如上述注释中的建议, http://linuxcommand.org/man_pages/xxd1.html )将两个脚本文件(用python创建和一个手动编写)转换为十六进制

xxd script.txt output.txt

I then compared the output for the two files with diff and could see an additional character 0a in hex at the end of the file. 然后,我将两个文件的输出与diff并在文件末尾看到一个附加的十六进制字符0a This corresponds to a newline. 这对应于换行符。 As detailed here Why should text files end with a newline? 如此处所述, 为什么文本文件应以换行符结尾? and here VIM Disable Automatic Newline At End Of File , vi adds a newline character at the end of the file if there isn't one. VIM在文件末尾禁用自动换行符 ,如果没有, vi 在文件末尾添加换行符。 This was enabling the script to run. 这使脚本可以运行。 Without the newline character at the end (which I hadn't included in the python script), the script wouldn't run. 最后没有换行符(我没有包含在python脚本中),该脚本将无法运行。

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

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