如何将bash命令替换输出包装在引号中

时间:2018-10-20 16:32:18

标签: bash sh

尝试从bash的括号()中找到的文件中提取字符串-命令替换。我正在使用此命令

strings $(locate '.tps' |egrep -i customer | egrep -i billing)

它对于没有空格的文件可以正常工作。当文件中有空格时,就会发生这种情况

strings: '/sales/rep/company/October': No such file
strings: 'bill': No such file
strings: 'and': No such file
strings: 'invoice': No such file
strings: 'customer': No such file
strings: 'name.tps': No such file

如果我尝试将命令用双引号引起来

strings "$(locate '.tps' |egrep -i customer | egrep -i billing)"

它将找到的所有文件串联为一个BIG文件名

1 个答案:

答案 0 :(得分:2)

使用GNU工具在ba​​sh中执行此操作可能类似于:

locate '.tps' | egrep -i customer | egrep -i billing | xargs -d $'\n' strings --

...或:

locate '.tps' | egrep -i customer | egrep -i billing | while IFS= read -r file; do
  strings "$file"
done

也就是说,使用换行符分隔的流来表示文件名列表 并不安全,因为允许通用UNIX文件系统(包括ext3,ext4,btrfs等)上的文件包含换行符本身。使用GNU定位:

locate -0 '*.tps' | egrep -i --null-data '(customer.*billing|billing.*customer)' | xargs -0 strings --
相关问题