Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax error near unexpected token 'fi'

Tags:

bash

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?

like image 219
Calvin Koder Avatar asked Dec 14 '13 18:12

Calvin Koder


People also ask

What Is syntax error near unexpected token `('?

The error message syntax error near unexpected token `(' occurs in a Unix-type environment, Cygwin, and in the command-line interface in Windows. This error will most probably be triggered when you try to run a shell script which was edited or created in older DOS/Windows or Mac systems.

What does syntax error unexpected?

A parse error: syntax error, unexpected appears when the PHP interpreter detects a missing element. Most of the time, it is caused by a missing curly bracket “}”. To solve this, it will require you to scan the entire file to find the source of the error.


Video Answer


1 Answers

As well as having then on a new line, you also need a space before and after the [, which is a special symbol in BASH.

#!/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 
like image 145
jprice Avatar answered Oct 03 '22 17:10

jprice