2016-12-02 51 views
0

我试图检查某个目录中是否存在某些特定类型的文件,并基于该文件运行了几条脚本。这里是我的代码:根据bash中的目录中的模式检查多个文件时出错

#!/bin/bash 

if [ ! -f Homology_Search/*tested.out ] && [ ! -f Homology_Search/*mod.annotation.*sense.gff ] 
    for i in Homology_Search/*tested.out; do python ../../filter_lincRNA_sequences_annotation2.py $i $i.mod; done 
    for i in Homology_Search/*tested.out.mod; do python ../../filter_lincRNA_sequences_annotation3.py $i final_summary_table.tsv $i.sp.csv; done 
    Rscript /final_summary_table_gen2.3.R 
else if [ ! -f Homology_Search/*tested.out ] 
    for i in Homology_Search/*tested.out; do python ../../filter_lincRNA_sequences_annotation2.py $i $i.mod; done 
    for i in Homology_Search/*tested.out.mod; do python ../../filter_lincRNA_sequences_annotation3.py $i final_summary_table.tsv $i.sp.csv; done 
    Rscript /final_summary_table_gen2.R 
else if [ ! -f Homology_Search/*mod.annotation.*sense.gff ] 
    Rscript /final_summary_table_gen3.R 
fi 

因为我有满足这些首要条件Homology_Search/*tested.outHomology_Search/*mod.annotation.*sense.gff,所以我希望得到一个输出文件的文件这个特殊的情况下 - 在环路final_summary_table.mod.tsv从最后的命令。但是我得到这个错误:

test.sh: line 7: syntax error near unexpected token `else' 
test.sh: line 7: `else if [ -f ! Homology_Search/*tested.out ]' 

我通过shell-check跑,我在我的代码固定的几个错误,在这里被更新的代码

#!/bin/bash 

if [ ! -f "../Homology_Search/*tested.out" ] && [ ! -f "../Homology_Search/*mod.annotation.*sense.gff" ]; then 
    for i in ../Homology_Search/*tested.out; do python ../../filter_lincRNA_sequences_annotation2.py "$i" "$i".mod; done 
    for i in ../Homology_Search/*tested.out.mod; do python ../../filter_lincRNA_sequences_annotation3.py "$i" final_summary_table.tsv "$i".sp.csv; done 
    Rscript final_summary_table_gen2.3.R 
elif [ ! -f "../Homology_Search/*tested.out" ]; then 
    for i in ../Homology_Search/*tested.out; do python../../filter_lincRNA_sequences_annotation2.py "$i" "$i".mod; done 
    for i in ../Homology_Search/*tested.out.mod; do python../../filter_lincRNA_sequences_annotation3.py "$i" final_summary_table.tsv "$i".sp.csv; done 
    Rscript final_summary_table_gen2.R 
else 
    Rscript final_summary_table_gen3.R 
fi 

它修复了大部分的问题,但仍存在在该else循环的问题不工作,当我试图拥有了只包含../Homology_Search/*mod.annotation.*sense.gff文件来测试else条件

Traceback (most recent call last): 
    File "../../filter_lincRNA_sequences_annotation2.py", line 9, in <module> 
    with open(infile, 'rU') as fh_in: 
IOError: [Errno 2] No such file or directory: '../Homology_Search/*tested.out' 
目录它不断给人一种错误

我在这里做错了什么?

+2

您错过了几个'then's。另外,我认为你的意思是'[! -f文件]而不是相反。 – Biffen

+4

考虑通过[shellcheck](http://www.shellcheck.net)运行它 –

+1

测试命令'[]'不支持带有选项'-f'的文件通配符。如果文件存在,您必须以另一种方式检查。 – infotoni91

回答

1

鉴于您的bash内置了compgen,您可以使用以下版本的脚本。由于测试命令[ -f filename ]不支持fileglobbing,你必须使用类似compgen:

#!/bin/bash 

if ! compgen -G Homology_Search/*tested.out > /dev/null && ! compgen -G Homology_Search/*mod.annotation.*sense.gff > /dev/null 
then 
    for i in Homology_Search/*tested.out; do python../../filter_lincRNA_sequences_annotation2.py "$i" "$i.mod"; done 
    for i in Homology_Search/*tested.out.mod; do python../../filter_lincRNA_sequences_annotation3.py "$i" "final_summary_table.tsv" "$i.sp.csv"; done 
    Rscript final_summary_table_gen2.3.R 
elif ! compgen -G Homology_Search/*tested.out > /dev/null 
then 
    for i in Homology_Search/*tested.out; do python../../filter_lincRNA_sequences_annotation2.py "$i" "$i.mod"; done 
    for i in Homology_Search/*tested.out.mod; do python../../filter_lincRNA_sequences_annotation3.py "$i" "final_summary_table.tsv" "$i.sp.csv"; done 
    Rscript final_summary_table_gen2.R 
elif ! compgen -G Homology_Search/*mod.annotation.*sense.gff > /dev/null 
then 
    Rscript final_summary_table_gen3.R 
fi 
+0

太棒了,非常感谢。 。我修改了一下代码,效果很好。 – upendra

0

这里是回答我的问题,如果有人发现有用的。特别感谢@ A.Grieco提供的宝贵提示

#!/bin/bash 

if compgen -G "../Homology_Search/*tested.out" > /dev/null && compgen -G "../Homology_Search/*mod.annotation.*sense.gff" > /dev/null 
then 
    for i in ../Homology_Search/*tested.out; do python ../../filter_lincRNA_sequences_annotation2.py "$i" "$i".mod; done 
    for i in ../Homology_Search/*tested.out.mod; do python ../../filter_lincRNA_sequences_annotation3.py "$i" final_summary_table.tsv "$i".sp.csv; done 
    Rscript final_summary_table_gen2.3.R 

elif compgen -G "../Homology_Search/*tested.out" > /dev/null && ! compgen -G "../Homology_Search/*mod.annotation.*sense.gff" > /dev/null 
then 
    for i in ../Homology_Search/*tested.out; do python ../../filter_lincRNA_sequences_annotation2.py "$i" "$i".mod; done 
    for i in ../Homology_Search/*tested.out.mod; do python ../../filter_lincRNA_sequences_annotation3.py "$i" final_summary_table.tsv "$i".sp.csv; done 
    Rscript final_summary_table_gen2.R 

elif ! compgen -G "../Homology_Search/*tested.out" > /dev/null && compgen -G "../Homology_Search/*mod.annotation.*sense.gff" > /dev/null 
then 
    Rscript final_summary_table_gen3.R 

fi