Running shell script from python
<p>I have written a small python programme which writes a C-shell script, and then calls the script with subprocess. However, python fails to execute the script. I tested this in the terminal and discovered that the script will also not run from the terminal (i.e. not a python issue). Further testing revealed I could write an identical script in a text editor (vi) and then successfully execute it from the terminal (i.e. I don't think the contents of the script are at fault). Eventually I found that if I tried to run the output from python directly in the terminal with <code>./myscript.com</code> it would <em>not</em> run. However, if I open with vi, make <em>NO</em> changes, save and then run from the terminal using <code>./myscript.com</code> (i.e. the exact same command) it will execute. 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. (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.</p>

<pre><code>#!/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
</code></pre>

<p>In case this is useful I used <code>xxd</code> (as suggested in the comments above, <a href="http://linuxcommand.org/man_pages/xxd1.html" rel="nofollow">http://linuxcommand.org/man_pages/xxd1.html</a>) to convert both script files, one created in python and one written manually, to hex</p>

<pre><code>xxd script.txt output.txt
</code></pre>

<p>I then compared the output for the two files with <code>diff</code> and could see an additional character <code>0a</code> in hex at the end of the file. This corresponds to a newline. As detailed here <a href="http://stackoverflow.com/questions/729692/why-should-text-files-end-with-a-newline">Why should text files end with a newline?</a> and here <a href="http://stackoverflow.com/questions/1050640/vim-disable-automatic-newline-at-end-of-file">VIM Disable Automatic Newline At End Of File</a>, <code>vi</code> adds a newline character at the end of the file if there isn't one. 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.</p>

python shell unix vi csh