0

I find a list of bam files using

$bams=$(find /data/and/ -type f -regex ".*\.bam")
#/data/DNA128441.bam
#/data/DNA128442.bam
#/data/DNA128443.bam

And wanted to feed in a script test.sh

#!/bin/bash
bam="$@"
echo "You provided the arguments:" ${bam}
s=${bam##*/}
r1=${s%.bam}_R1.fastq
r2=${s%.bam}_R2.fastq
echo $r1
echo $r2

But when I run:

$bams=$(find /data/and/ -type f -regex ".*\.bam")
./test.sh $bam
You provided the arguments: /data/and/DNA128441.bam /data/and/DNA128342.bam /data/and/DNA128343.bam
DNA128343_R1.fastq
DNA128343_R2.fastq

My question is how to make it process one by one such like a desired output:

$bams=$(find /data/and/ -type f -regex ".*\.bam")
./test.sh $bam
You provided the arguments: /data/and/DNA128341.bam
DNA128341_R1.fastq
DNA128341_R2.fastq
You provided the arguments: /data/and/DNA128342.bam
DNA128342_R1.fastq
DNA128342_R2.fastq
You provided the arguments: /data/and/DNA128343.bam
DNA128343_R1.fastq
DNA128343_R2.fastq
David Z
  • 6,641
  • 11
  • 50
  • 101
  • You should not use anything like `bam="$@"`, since it mashes all of the arguments together into a single string separated by spaces. Re-splitting it into separate arguments can be ambiguous in several ways. Either just use `"$@"` directly, or save the args as an array with `bam=("$@")`. – Gordon Davisson Jul 02 '20 at 22:22
  • as a one-liner you can do this via find exec paramter `find /dir/to/data -type f -name "*.bam" -exec bash -c 'echo -e "found {}\n$(basename {} .bam)_R1.fq\n$(basename {} .bam)_R2.fq"' \;` – pyr0 Jul 03 '20 at 10:33

0 Answers0