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