I have written the following shell script:
#!/bin/bash
counter=0
while [ $counter -lt 250 ]; do
perl Cumulative_Percentage.pl TP53.lst T1_endo.maf >> OutFile
perl Optimize_Panel.pl TP53.LST T1_endo.maf >> TP53.lst
let counter=counter+1
done
And it works well. The problem is that it often runs through far more loops than it needs to. The Perl script, Optimize_Panel.pl, prints a single line if the loop should continue or exits without printing if the loop should stop. I want the while loop to terminate if Optimize_Panel.pl exits instead of printing... but how?
One possible solution I can imagine is to run
wc TP53.lst
at the beginning and end of the loop; first setting it to a variable then checking to see if it had increased then leading it to a premature done statement if TP53.lst had not been appended. I'm confident that this could work but it feels clunky and I suspect there is a much simpler way to modify the shell script.
Another way I can think of doing this would be to pipe the output of Optimize_Panel.pl into a temporary file then somehow check to see if that file was empty.
Any ideas?
You can capture the output in a variable, then break out of the loop if that variable is zero-length.
while [ $counter -lt 250 ]; do
perl Cumulative_Percentage.pl TP53.lst T1_endo.maf >> OutFile
output=$(perl Optimize_Panel.pl TP53.LST T1_endo.maf)
[ -z "$output" ] && break
echo "$output" >> TP53.lst
let counter=counter+1
done