Syntax error near unexpected token 'fi'

Viewed 172298

I'm trying to write a script that removes all the .jpg's that end in an odd number. This is my code:

#!/bin/bash
echo "start\n"
for f in *.jpg
do
  fname=$(basename "$f")
  echo "fname is $fname\n"
  fname="${filename%.*}"
  echo "fname is $fname\n"
  if[$((fname %  2)) -eq 1 ] then
    echo "removing $fname\n"
    rm $f
  fi
done

When I run it it outputs start and then says "syntax error near unexpected token 'fi'"

When I had then on the line after if it said "syntax error near unexpected token 'then'"

How do i fix this?

5 Answers

@sagar's answer solved my problem (Save file to UNIX format), please note that if you are using VSCode, you can select the file's end of line sequence (LF or CRLF) at the bottom right.

Also see this CRLF vs LF if you would like to understand the why. Common problem when you are working on different OS.

Related