本文介绍了庆典:for循环,两个变量挂钩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的问题;两个目录,包含:

I've the following problem; two directories that containing:


  • DIR1:类似以下文件的列表:

file1.fq file2.fq file3.fq

file1.fq file2.fq file3.fq

等..


  • DIR2:文件的LIS类似以下内容:

file1.fq.sa file2.fq.sa file3.fq.sa

file1.fq.sa file2.fq.sa file3.fq.sa

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

what I have to do is running a command that uses file1.fq and file1.fq.sa together.

我试过下面的循环:

fq=dir1/*
sa=dir2/*

for fqfiles in $fq;

do

for sa_files in $sa;

do

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

done

done

问题是,我的循环执行以下语句:

The problem is that my loop execute the following:

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

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

等等......在几乎无限循环!

and so on...in a almost infinite loop!

我希望我的循环可以产生类似:

I wish my loop could produces something like:

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

等等......

etc...

您能帮我吗?

感谢您!

法比奥

推荐答案

您可以遍历 DIR1 ,使用的上的文件,然后preFIX与 DIR2 并添加您需要的扩展。您可能还需要检查这些文件在第二个目录,并运行命令仅在这两个文件都可以

You can loop over dir1, use basename on the files and then prefix with dir2 and append the extension you need. You might also want to check for the files in the second directory and run your command only if both files are available

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

如果您不想要的目录部分,用这个来代替

If you don't want the directory parts, use this one instead

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

这篇关于庆典:for循环,两个变量挂钩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 00:52