1

I have the following bash script:

#!/bin/bash

for r in $(find . -name "*.fastq");
do

  cat <<EOF
  #qsub <<EOF
#!/bin/bash -l

#PBS -N $r

EOF

done

Unfortunately, all filename get split:

> sh  cFiltering_pbs.sh 
  #qsub <<EOF
#!/bin/bash -l

#PBS -N ./76A

  #qsub <<EOF
#!/bin/bash -l

#PBS -N Paired.fastq

  #qsub <<EOF
#!/bin/bash -l

#PBS -N ./104A

  #qsub <<EOF
#!/bin/bash -l

#PBS -N Paired.fastq

Here is the find output:

find . -name "*.fastq"
./76A Paired.fastq
./104A Paired.fastq

What did I miss?

user977828
  • 921
  • 4
  • 16
  • 30
  • 5
    `for r in $(find . -name "*.fastq");` is [pretty bad...](https://unix.stackexchange.com/q/321697/22142) – don_crissti Jan 17 '19 at 23:08
  • 1
    Are all your `fastq` files in the same directory? – roaima Jan 17 '19 at 23:15
  • yes, they're all in the same directory. I solved it with this https://unix.stackexchange.com/a/9499/34872 – user977828 Jan 17 '19 at 23:26
  • That's a poor solution for your situation – roaima Jan 17 '19 at 23:36
  • 2
    see: [Why does my shell script choke on whitespace or other special characters?](https://unix.stackexchange.com/questions/131766/why-does-my-shell-script-choke-on-whitespace-or-other-special-characters) and [Recursively iterate through files in a directory](https://unix.stackexchange.com/questions/139363/recursively-iterate-through-files-in-a-directory) and also http://mywiki.wooledge.org/WordSplitting – ilkkachu Jan 17 '19 at 23:37

1 Answers1

2

Instead of using find you can simply loop through the files themselves. Notice that I have double-quoted "$r"; you need to do that to keep the space in the filenames intact and unparsed by the shell.

for r in *.fastq
do
    # Stuff using "$r"...
    :
done
roaima
  • 107,089
  • 14
  • 139
  • 261