Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby,使用backticks grep文件扩展名,并在文件内容中搜索字符串_Ruby_Bash_Grep_Backticks - Fatal编程技术网

Ruby,使用backticks grep文件扩展名,并在文件内容中搜索字符串

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字符串 它不起作用 有没有办法让它那样工作

我试图找到在内容中包含“一些字符串用户输入”的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字符串

它不起作用

有没有办法让它那样工作


提前感谢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