'Nested loop using tool in bash [duplicate]

I have a loop I am trying to construct. that is pulling files from different paths. I need to loop these together. Here is what I have:

#!/bin/bash
#SBATCH --mem=110g
#SBATCH --cpus-per-task=12

module load java/17.0.2

for bam in /PATH1/*.sorted.dmark.bam;
do
  java -Xmx110g -jar /PATHtoTOOL/fgbio-2.0.0.jar AnnotateBamWithUmis \
     -i bam \
     -f /PATH2/*_L001_UMI.fastq.gz \ # This is where I need the secodary loop
     -o bam.UMI.bam
done


Solution 1:[1]

Populate two arrays with the files, walk them by index:

#! /bin/bash
dmarks=(/PATH1/*.sorted.dmark.bam)
fastqs=(/PATH2/*_L001_UMI.fastq.gz)

if (( ${#dmarks[@]} != ${#fastqs[@]} )) ; then
    echo Different number of files >&2
    exit 1
fi

for (( i=0; i<${#dmarks[@]}; ++i)) ; do
    dmark=${dmarks[i]}
    fastq=${fastqs[i]}
    java -Xmx110g -jar /PATHtoTOOL/fgbio-2.0.0.jar AnnotateBamWithUmis \
        -i "$dmark" \
        -f "$fastq" \
        -o bam.UMI.bam
done

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1