在 Bash 中循环查找输出,其中文件名包含空格 [英] Looping through find output in Bash where file name contains white spaces

查看:25
本文介绍了在 Bash 中循环查找输出,其中文件名包含空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试搜索可能包含空格的文件我尝试使用 -print0 并设置 IFS 这里是我的脚本

I try to search for files that may contain white spaces I try to use -print0 and set IFS here is my script

IFS=$'';find people -name '*.svg' -print0 | while read file; do
    grep '<image' $file > /dev/null && echo $file | tee -a embeded_images.txt;
done

我尝试对所有包含嵌入图像的 svg 文件进行优化,它在没有 -print0 的情况下工作,但一个文件失败,所以我停止了脚本.这是一个更简单的例子,但也不起作用

I try to fine all svg file containing embeded images, it work without -print0 but fail one one file so I stop the script. Here is simpler example that don't work too

IFS=$'';find . -print0 | while read file; do echo $file; done

它不显示任何内容.

推荐答案

使用 read -d '' -r file 并且只为 IFS>阅读:

Use read -d '' -r file and set IFS only for the context of read:

find people -name '*.svg' -print0 | while IFS= read -d '' -r file; do
    grep '<image' "$file" > /dev/null && echo "$file" | tee -a embeded_images.txt;
done

并引用您的变量.

这篇关于在 Bash 中循环查找输出,其中文件名包含空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆