Bash脚本:通过文件夹多个文件扩展循环,忽略不存在的文件扩展名 [英] Bash script: Looping through multiple file extensions in folder, ignoring file extensions that don't exist
本文介绍了Bash脚本:通过文件夹多个文件扩展循环,忽略不存在的文件扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有图片,其中可以包含JPEG,JPG和PNG文件的目录。我想通过他们回路bash脚本。其可能的目录不包含任何PNG文件,或JPEG文件或JPG文件。如果文件扩展名不存在,我的脚本给出了一个错误。如何忽略这些?
在文件* {JPG,JPEG,PNG};做
回声$文件
解决方案
您可以检查文件是否存在:
在文件* {JPG,JPEG,PNG};做
[-e$文件] ||继续
#这里的$文件的存在
DONE
您也可以使用 禁用了javascript -s nullglob
,这将避免扩大到不存在的文件:
禁用了javascript -s nullglob
。在*文件{JPG,JPEG,PNG};做
#这里的$文件的存在
DONE
I have a directory of pictures, which can contain jpeg, jpg, and png files. I want to loop through them in a bash script. Its possible for the directory not to contain any png files, or jpeg files, or jpg files. If a file extension doesn't exist, my script gives an error. How to ignore these?
for file in *.{jpg,jpeg,png} ; do
echo $file
解决方案
You can check if the file exists:
for file in *.{jpg,jpeg,png}; do
[ -e "$file" ] || continue
# Here "$file" exists
done
You can also use shopt -s nullglob
which will avoid expanding to non existing files:
shopt -s nullglob
for file in *.{jpg,jpeg,png}; do
# Here "$file" exists
done
这篇关于Bash脚本:通过文件夹多个文件扩展循环,忽略不存在的文件扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文