Syntax error near unexpected `if 'token
I am currently trying to write a bash script that helps me traverse a directory and check .jpeg or .jpg extensions for files. I came up with the following:
#declare $PICPATH, etc...
for file in $PICPATH
if [ ${file -5} == ".jpeg" -o ${file -4} == ".jpg" ];
then
#do some exif related stuff here.
else
#throw some errors
fi
done
After execution, bash keeps throwing an error on the if line: "Syntax error around unexpected token` if '.
I'm completely new to scripting; what is wrong with my if statement?
Thank.
+3
source to share
2 answers
${file -5}
is a syntax error. Maybe you mean
${file#*.}
?
Better anyway:
for file in $PICPATH; do
image_type="$(file -i "$file" | awk '{print gensub(";", "", $2)}')"
case $image_type in
image/jpeg)
# do something with jpg "$file"
;;
image/png)
# do something with png "$file"
;;
*)
echo >&2 "not implemented $image_type type "
exit 1
;;
esac
done
If you only want to process files jpg
, follow these steps:
for file in $PICPATH; do
image_type="$(file -i "$file" | awk '{print gensub(";", "", $2)}')"
if [[ $image_type == image/jpeg ]]; then
# do something with jpg "$file"
fi
done
+2
source to share