2013-02-26 54 views
1

我遇到以下問題;兩個目錄,包含:bash:for循環,鏈接兩個變量

  • DIR1:

file1.fq file2.fq file3.fq

等..

    :類似下面的文件列表
  • dir2:下面這樣的文件:

f ile1.fq.sa file2.fq.sa file3.fq.sa

什麼,我要做的就是運行使用file1.fq和file1.fq.sa在一起的命令。

我嘗試了以下循環:

fq=dir1/* 
sa=dir2/* 

for fqfiles in $fq; 

do 

for sa_files in $sa; 

do 

mycommand ${sa_files} ${fqfiles} > ${sa_files}.cc & 

done 

done 

的問題是,我的循環執行以下命令:

mycommand file1.fq.sa file1.fq > file1.fq.sa.cc #correct 

而且

mycommand file1.fq.sa file2.fq > file1.fq.sa.cc #wrong! 

等..在幾乎無限的循環中!

我希望我的循環可以產生類似:

mycommand file1.fq.sa file1.fq > file1.fq.sa.cc 
mycommand file2.fq.sa file2.fq > file2.fq.sa.cc 
mycommand file3.fq.sa file3.fq > file3.fq.sa.cc 

等等

你能幫幫我嗎?

謝謝!

法比奧

回答

1

您可以遍歷dir1,使用上的文件basename,然後用dir2前綴和附加您所需要的擴展。您可能還需要檢查這些文件在第二個目錄,並運行你的命令只有當這兩個文件都可以

for f in dir1/*.fq; do 
    b=$(basename "$f") 
    f2=dir2/"$b".sa 
    if test -f "$f2"; then 
     mycommand "$f2" "$f" >"$b".sa.cc 
    fi 
done 

如果你不想目錄部分,用這一個,而不是

mycommand "$b".sa "$b" >"$b".sa.cc 
+0

大!有用!謝謝。我將詳細研究basename選項! – fabioln79 2013-02-26 12:31:09