0

I am writing a Python script, in which I create then execute a shell file.

This shell file is composed of several lines, each line being a shell command (with some grep, sort, etc..). Here is an example of one of these command lines from my_shell_file.sh :

nohup grep -A1 "pattern" myfile.fastq | grep -v "pattern2" | grep -v "^pattern3" | grep "^pattern4" | sort -T ./tmp | uniq -c > Result_directory/result_file.txt &

The only thing that changes between each line is the pattern4 and the name of the result_file.txt.

Well, my Python script creates my_shell_file.sh very well, it contains all the command lines I expected and without any syntax error. Problem comes at next step, when trying to run this shell file with :

os.system("sh ./my_shell_file.sh")

I expect one result_file.txt per command line that I wrote in the my_shell_file.sh. Problem is that not all the command lines from my_shell_file.sh are run.

There is always a few lines, the last ones, that are never run, I don't know why. It seems very strange because 1) that means Python can run my_shell_file.sh, so why doesn't he run the last lines 2) I see no syntax difference between each line of my_shell_file.sh, so I don't know why it stops a few lines before the end, 3) the line where it stops can be different from one try to another...

Sometimes in my log files, I see messages like :

./my_shell_file.sh: line 63: Premature EOF while searching for the correspondant « " »
./my_shell_file.sh: line 64: Syntax error: Premature EOF

It looks like an « " » is not closed, buuuut... I see no syntax error... Plus if I just change the order of the lines in my_shell_file.sh, the issue can be resolved...

Whatsmore if I myself run the my_shell_file.sh created by Python,there is no problem, all command lines are run !

Does anyone have an idea about why the last lines are randomly skipped when the script is running via Python ?

Thank you in advance

Python version : 2.6.6

Totoro
  • 97
  • 2
  • 7
  • Can you include the generated script that is failing? – Etan Reisner Mar 06 '15 at 14:50
  • Did you actually close() the shell script before running the system() call? – Yann Vernier Mar 06 '15 at 15:27
  • Yann : Yes, I close() the shell script before creating it. Whatsmore my Python script is made of 2 modules to call separately. The first one creates the shell script, and the second one executes it. Hence the user has to run the Python script twice, and Python exits between the first and the second module. Etan, I can't find where to add attachment file to the topic. I guess if I just copy/paste the entire shell script to a comment, it is not good ? – Totoro Mar 06 '15 at 15:41

0 Answers0