0

I was trying to improve a function definition using flycheck (shellcheck). The commented line on the code below is what I tried to improve. The code works but I get the error message syntax error near unexpected token ('`

my_loop() {
# for ifastq in $(ls $1 | grep -v txt)
for ifastq in $1/*.f?(ast)q?(.gz)
do
echo "$ifastq"
done
}

Is the code above more fragile than the one using the commented line? Should I also include shopt -s extglob in the function definition or at the top of the script?

Thanks

BTW: the code is supposed to capture files with .fastq .fq fq.gz or .fastq.gz extensions

user25482
  • 23
  • 1
  • 3

1 Answers1

0

Is the code above more fragile than the one using the commented line?

No, the opposite. It's generally a bad idea to process the output of ls. If any of the filenames contains whitespace, it will be split into multiple words when you use $(ls ...).

You do need to enable extglob, since it's not enabled by default.

Extended globbing is a bash extension, Flycheck might not recognize it. Emacs SE would be a better place to ask questions specific to this Emacs extension.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thanks for the comments. Should I then, post the question on Emacs SE and delete this one? – user25482 Dec 15 '22 at 23:36
  • The part specifically about Flycheck seems like it would be appropriate there. But you also asked about the choice of ways to loop, and that fits here. – Barmar Dec 16 '22 at 00:26