2013-02-26 64 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