简体   繁体   English

根据管道输出终止Bash脚本

[英]Terminate Bash script dependent on piped output

I have written the following shell script: 我编写了以下shell脚本:

#!/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. 如果循环应继续,则Perl脚本Optimize_Panel.pl将打印一行;如果循环应停止,则退出而不打印而退出。 I want the while loop to terminate if Optimize_Panel.pl exits instead of printing ... but how? 我想如果Optimize_Panel.pl退出而不是打印,则while循环终止 ...但是如何?

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. 首先将其设置为变量,然后检查其是否增加,然后如果未附加TP53.lst,则将其引导至过早的done语句。 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. 我相信这可以解决问题,但感觉很笨拙,我怀疑有一种更简单的方法来修改Shell脚本。

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. 我可以想到的另一种方法是将Optimize_Panel.pl的输出传递到一个临时文件中,然后以某种方式检查该文件是否为空。

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

Simple hack (in case the perl script doesn't produce meaningful exit status): 简单的破解(以防perl脚本不产生有意义的退出状态):

perl Optimize_Panel.pl TP53.LST T1_endo.maf | grep . >> TP53.lst || break

grep . matches any non-empty line, so it is a way of removing empty lines. 匹配任何非空行,因此这是一种删除空行的方法。 But it is also a way of detecting whether there were any non-empty lines; 但这也是一种检测是否有非空行的方法。 it will fail precisely in the case where there weren't any. 如果没有,它将完全失败。 Of course, it would be much better if the perl script provided an appropriate exit status code (0 for success; non-zero for failure), in which case you could leave out the | grep . 当然,如果perl脚本提供适当的退出状态代码(成功则为0,失败则为非零)会更好,在这种情况下,您可以省略| grep . | grep . hack.) hack。)

By the way, you can use bash's arithmetic for statement to make your script possibly more readable: 顺便说一句,您可以使用bash的算术语句来使脚本更具可读性:

for ((counter=0; counter < 250; ++counter)); do
    perl Cumulative_Percentage.pl TP53.lst T1_endo.maf >> OutFile
    perl Optimize_Panel.pl TP53.LST T1_endo.maf |
      grep . >> TP53.lst || break;
done

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

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