Ruby,使用backticks grep文件扩展名,并在文件内容中搜索字符串
我试图找到在内容中包含“一些字符串用户输入”的txt和html文件 在命令行中,下面的命令非常有效Ruby,使用backticks grep文件扩展名,并在文件内容中搜索字符串,ruby,bash,grep,backticks,Ruby,Bash,Grep,Backticks,我试图找到在内容中包含“一些字符串用户输入”的txt和html文件 在命令行中,下面的命令非常有效 grep -ri --include \*.txt --include \*.html string . 但在尝试将其编码到ruby程序时,如下所示: s = ARGV[0] files = %x(grep -ri --include \*.txt --include \*.html #{s} .) 这就是我运行程序的方式 $ruby app.rb字符串 它不起作用 有没有办法让它那样工作
grep -ri --include \*.txt --include \*.html string .
但在尝试将其编码到ruby程序时,如下所示:
s = ARGV[0]
files = %x(grep -ri --include \*.txt --include \*.html #{s} .)
这就是我运行程序的方式
$ruby app.rb字符串
它不起作用
有没有办法让它那样工作
提前感谢Ruby中的Escape,它不需要转义,因此传输到shell的文本是
grep-ri--include*.txt--include*.html文件名。
。转义反斜杠,使shell保持原样:
files = %x(grep -ri --include \\*.txt --include \\*.html #{s} .)
你可以用|代替太好了!!成功了。原来我是在逃跑,就像你说的。谢谢,这句话也可以,但要解决我之前遇到的问题,我必须这样说:(-include\*.txt--include\*.html),就像@Amadan说的那样。谢谢你的帮助
#!/bin/env ruby
# encoding: utf-8
s = ARGV[0]
files = %x|grep -ri --include \*.txt --include \*.html #{s} .|
puts files