0

I'm trying to run this for loop but I'm getting the same error message: -bash: [: missing `]'

for FILE in *.fasta
do
  a=$(grep -c ">" $FILE)
  if [ "$a" == 15] 
  then
    mv $FILE folder/
  fi
done

Any help will be appreciated.

Thanks!

Santiago
  • 57
  • 6
  • http://shellcheck.net/ would have caught this for you automatically. – Charles Duffy Feb 28 '17 at 17:01
  • ...and btw, you've got more bugs than just the one discussed in the linked code -- really *do* run your code through shellcheck. And in the future, use the `{}` button in the SO editor or four-space indents to format code. – Charles Duffy Feb 28 '17 at 17:02
  • (the linked question has a different error because they left out the space on the left-hand `[` as well as the right-hand `]`, but the fix is exactly the same. And btw, `==` actually isn't valid in POSIX sh -- it's a ksh/bash extension to allow it, but if you want to write compatible code, get used to using `=` for string comparisons in shell instead). – Charles Duffy Feb 28 '17 at 17:05
  • (also, all-upper-case names are used with variables meaningful to the operating system or shell; to avoid conflicts, use lowercase names for your own variables. See the POSIX spec at http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html, fourth paragraph, specifying lower-case names to be reserved for application use -- while that spec is on environment variables, it applies to regular shell variables as well, since writing to a shell variable will automatically overwrite any like-named environment variable). – Charles Duffy Feb 28 '17 at 17:06
  • (...and quotes are *important*. A file named `-i hello.fasta`, for instance, would pass the `-i` argument to `mv`, unless you fix your code to put double-quotes around the expansion for its name). – Charles Duffy Feb 28 '17 at 17:08
  • Thank you very much, I'm just beginning to learn coding. I didn't know about shellcheck! very usefull – Santiago Feb 28 '17 at 17:10

0 Answers0