Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
324 views
in Technique[技术] by (71.8m points)

Looping through find output in Bash where file name contains white spaces

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

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

it don't display anything.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Though Dennis Williamson's answer is absolutely correct, it creates a subshell, which will prevent you from setting any variables inside the loop. You may consider using process substitution, as so:

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

The first < indicates that you're reading from a file, and the <(find...) is replaced by a filename (usually a handle to a pipe) that returns the output from find directly. Because while reads from a file instead of a pipe, your loop can set variables that are accessible from outside the scope.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...