在 bash 命令中两个两个地引用文件

making reference to file two by two in a bash command

我有这个文件列表,我必须成对分析(a_1 和 a_2,b_1 和 b_2 等等)

a_1.fq
a_2.fq
b_1.fq
b_2.fq
c_1.fq
...

我想设置一个for循环来在命令中引用这些文件对,如下 这只是我想做的一个例子(语法错误):

$ for File1 File2 in *1.fq *2.fq; do STAR --readFilein File1 File2 ; done

非常感谢您的帮助

您可以只迭代一种类型的文件并使用参数扩展来设置第二种类型:

for file1 in *1.fq; do
    file2=${file1%1.fq}2.fq
    ...

%pattern 删除变量值末尾的模式。

您可能想在 运行 命令之前检查文件 2 是否存在。

或者,如果您可以获得按对相邻的方式列出的文件,则可以用它们填充 $@shift 两个参数:

set -- [a-z]_[12].fq
while (( $# )) ; do
    file1=
    file2=
    shift 2
    ...
done

使用函数处理对并迭代 glob 扩展:

process_pair()
{
  while [ $# -gt 0 ]
  do
    f1= # get 1st argument
    shift # shift to next argument
    f2= # get 2nd argument
    shift # shift to next for the next round

    # Do stuffs with file1 and file2
    printf 'f1=%s\tf2=%s\n' "$f1" "$f2"
  done
}

# Submit pattern expansion as process_pair arguments
process_pair ./*_[12].fq

您可以尝试类似的方法:

for letter in {a..z}
do
 # Your logic
  echo "Working on $letter"
  cat $letter\_1.fq
  cat $letter\_2.fq
done