小编pfm*_*e60的帖子

在 find/grep 搜索结果中保留换行符

我正在使用以下脚本递归搜索从其他文件调用的 php 脚本的出现。

find . -exec grep -Hn $1 {} \; | grep -v ^Binary;
Run Code Online (Sandbox Code Playgroud)

效果很好!现在,我需要返回的结果来确定下一步要采取的行动。

r=$(find . -exec grep -Hn $str {} \; | grep -v ^Binary;)

 if [ -z "$r" ];
    then
          Do this
    else
          Do something else
    fi
Run Code Online (Sandbox Code Playgroud)

问题: find 脚本通过它自己返回结果,每个结果都在一个新行上。

./path/to/file.php
./path/to/another_file.php
./path/to/third_file.php
Run Code Online (Sandbox Code Playgroud)

但是,将输出分配给 $r 变量时,不会保留换行符,结果打印在一行上,因此难以阅读。

./path/to/file.php ./path/to/file.php ./path/to/third_file.php
Run Code Online (Sandbox Code Playgroud)

如何在将输出分配给变量时保留换行符?

bash grep find newlines

5
推荐指数
1
解决办法
8018
查看次数

标签 统计

bash ×1

find ×1

grep ×1

newlines ×1