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

后端 未结 2 1158
广开言路
广开言路 2020-11-30 10:07

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



        
2条回答
  •  臣服心动
    2020-11-30 10:44

    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 ' /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.

提交回复
热议问题