gpt4 book ai didi

linux - Bash 脚本 : Looping through multiple file extensions in folder, 忽略不存在的文件扩展名

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:45:01 24 4
gpt4 key购买 nike

我有一个图片目录,其中可以包含jpeg、jpg 和png 文件。我想在 bash 脚本中循环遍历它们。该目录可能不包含任何 png 文件、jpeg 文件或 jpg 文件。如果文件扩展名不存在,我的脚本就会出错。如何忽略这些?

for file in *.{jpg,jpeg,png} ; do
echo $file

最佳答案

您可以检查文件是否存在:

for file in *.{jpg,jpeg,png}; do
[ -e "$file" ] || continue
# Here "$file" exists
done

您还可以使用 shopt -s nullglob这将避免扩展到不存在的文件:

shopt -s nullglob
for file in *.{jpg,jpeg,png}; do
# Here "$file" exists
done

关于linux - Bash 脚本 : Looping through multiple file extensions in folder, 忽略不存在的文件扩展名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37448166/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com