bash – Unix. Run script across multiple dirs on specific files, where pathname has regex

I want to make a bash script (split.sh) that iterates across multiple dirs with same suffix, and then runs a function for specific files within them. I am almost there:

#!/bin/bash
path="/mypath/MAP-9-[0-9][0-9][0-9]"

for filename in $path/*bam; do
    [ -e "$filename" ] || continue
    echo $filename
        for chrom in `seq 1 22` X Y
        do
        samtools view -bh $filename $chrom > $path/$chrom.bam
        samtools index > $path/$chrom.bam; 
        done
done

However, I get many messages of this kind: “split.sh: line 12: /mypath/MAP-9-[0-9][0-9][0-9]/6.bam: No such file or directory”

The problem is that the script is not recognizing the “[0-9][0-9][0-9]” regex part of the pathname. I also tried adding escape characters to the square brackets without success. It must be a very simple solution, but I am not able to solve it.

Read more here: Source link