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.
This is an extract of the tree command's output:
|-- [[
|-- MAP-9-001
| |-- MAP-9-001.bam
| `-- MAP-9-001.bam.bai
|-- MAP-9-003
| |-- MAP-9-003.bam
| `-- MAP-9-003.bam.bai
|-- MAP-9-005
| |-- MAP-9-095.bam
| `-- MAP-9-095.bam.bai
|-- split.sh