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?
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With