2012-08-13 56 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 
相關問題