我目前正在尝试编写一个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
执行时,bash不断在if行上抛出一个错误:“语法错误接近意外令牌`if'。
我是一个全新的脚本编写者;我的if语句出了什么问题?
感谢。
答案 0 :(得分:8)
我认为你只是缺少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
答案 1 :(得分:2)
${file -5}
是语法错误。也许你的意思是
${file#*.}
无论如何,做得更好:
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
如果您只想处理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