I am currently trying to write a bash script which helps me step through a directory and check for .jpeg or .jpg extensions on files. I've come up with the following:
我目前正在尝试编写一个bash脚本,它帮助我通过一个目录,并检查文件上的.jpeg或.jpg扩展。我想到了以下几点:
#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
Upon execution, bash keeps throwing a an error on the if line: "syntax error near unexpected token `if'.
在执行时,bash总是在if行上抛出一个错误:“如果”,则在“意外的令牌”附近出现语法错误。
I'm a completely new to scripting; what is wrong with my if statement?
我是一个全新的脚本;我的if语句有什么问题?
Thanks.
谢谢。
2 个解决方案
#1
7
I think you're just missing the do clause of the for
loop:
我想你只是错过了for循环的do子句:
#declare $PICPATH, etc...
for file in $PICPATH; do
if [ ${file -5} == ".jpeg" -o ${file -4} == ".jpg" ];
then
#do some exif related stuff here.
else
#throw some errors
fi
done
#2
2
${file -5}
is a syntax error. Maybe you mean
是一个语法错误。也许你的意思
${file#*.}
?
吗?
Anyway, better do :
无论如何,更好的做:
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 treat jpg
files, do :
如果你只想处理jpg文件,可以:
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
#1
7
I think you're just missing the do clause of the for
loop:
我想你只是错过了for循环的do子句:
#declare $PICPATH, etc...
for file in $PICPATH; do
if [ ${file -5} == ".jpeg" -o ${file -4} == ".jpg" ];
then
#do some exif related stuff here.
else
#throw some errors
fi
done
#2
2
${file -5}
is a syntax error. Maybe you mean
是一个语法错误。也许你的意思
${file#*.}
?
吗?
Anyway, better do :
无论如何,更好的做:
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 treat jpg
files, do :
如果你只想处理jpg文件,可以:
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