2012-08-13 57 views
2

我需要一个bash脚本来执行一个程序没有一个特定的文件,并在同directory.This程序创建的输出文件中的所有目录需要存在于每个目录与名称的输入文件* .DNA.fasta .Suppose我有可能包含子目录也一个bash脚本运行的目录程序不具有特定文件

dir1/a.protein.fasta 
dir2/b.protein.fasta 
dir3/anyfile 
dir4/x.orf.fasta 

我已经发现,不目录开始下列目录有一个特定的文件whic名是* .protein.fasta
在这种情况下,我想 在dir3dir4上市(因为它们不包含*.protein.fasta) 我曾尝试这样的代码:

find . -maxdepth 1 -type d \! -exec test -e '{}/*protein.fasta' \; -print 

但似乎我错过了一些东西它不工作。 也我不知道如何进行整个故事。

回答

2

这是一个棘手的一个。

我想不出一个好解决的。但是,这里有一个解决方案。需要注意的是保证这是工作,如果你的目录或文件名包含换行符,这是不能保证工作,如果它们包含其他特殊字符。 (我只测试过你的问题中的样本。)

此外,我还没有包括-maxdepth,因为你说你也需要搜索子目录。

#!/bin/bash 

# Create an associative array 
declare -A excludes 

# Build an associative array of directories containing the file 
while read line; do 
    excludes[$(dirname "$line")]=1 
    echo "excluded: $(dirname "$line")" >&2 
done <<EOT 
$(find . -name "*protein.fasta" -print) 
EOT 

# Walk through all directories, print only those not in array 
find . -type d \ 
| while read line ; do 
    if [[ ! ${excludes[$line]} ]]; then 
    echo "$line" 
    fi 
done 

对于我来说,这将返回:

. 
./dir3 
./dir4 

所有这些是不包含匹配*.protein.fasta一个文件目录。当然,你可以用最后的echo "$line"替换你需要做的这些目录。

或者:

如果你真正寻找的是刚刚的顶级目录不包含任何子目录中的匹配文件列表,下面的bash一行程序可能就足够了:

for i in *; do test -d "$i" && (find "$i" -name '*protein.fasta' | grep -q . || echo "$i"); done 
+0

耶大,这部分看起来不错,但不知道如何处理剩下的 – shaq 2012-08-13 11:53:41

+0

我需要用一个文件,该文件是在目录中我和它的名字运行程序的目录的名称加上.DNA.fasta **它是n ot以这种方式工作,我用过你有什么想法?**为我in *; do test -d“$ i”&&(find“$ i”-name'* protein.fasta'| grep -q。|| exec“myprogram”“$ i.DNA.fasta”);做 – shaq 2012-08-13 13:13:26

+0

如果您有已经运行新的测试,请[更新您的问题(http://stackoverflow.com/posts/11932067/edit),包括这些测试及其结果。当涉及到代码格式时,这样的评论绝对是吸引人的。另外,它是'* .DNS.fasta'就像你的问题,或'* .protein.fasta'就像你的例子?请澄清[在你的问题](http://stackoverflow.com/posts/11932067/edit)。 – ghoti 2012-08-13 14:06:26

0
#!/bin/bash 

for dir in *; do 

test -d "$dir" && (find "$dir" -name '*protein.fasta' | grep -q . ||  Programfoo"$dir/$dir.DNA.fasta"); 
done 
相关问题