首页 文章

bash:for循环,两个变量链接

提问于
浏览
1

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

  • dir1:以下文件列表:

file1.fq file2.fq file3.fq

等等..

  • dir2:以下文件:

file1.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 回答

  • 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
    

相关问题