bash 带引号的 Grep
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6946677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Grep with quotation mark
提问by dtbarne
I'm trying to scan an error log for lines with 503 errors, so I'm grepping for " 503(quote space 503).
我正在尝试扫描具有 503 错误的行的错误日志,所以我正在搜索" 503(引用空间 503)。
This seems simple, but it won't work:
这看起来很简单,但行不通:
grep '" 503 ' access.log
I get the following error:
我收到以下错误:
bash: -c: line 0: unexpected EOF while looking for matching `"' bash: -c: line 1: syntax error: unexpected end of file
bash:-c:第 0 行:寻找匹配的“”时出现意外 EOF bash:-c:第 1 行:语法错误:文件意外结束
采纳答案by dtbarne
The issue was due to some erroneous directives in .bashrc.
该问题是由于.bashrc.
回答by Micha? ?rajer
Seems like you are running it via some system() in some language, aren't you? Try:
似乎您正在通过某种语言的某种 system() 运行它,不是吗?尝试:
grep '\" 503 ' access.log
or:
或者:
grep "\" 503 " access.log
Directly in shell just grep '" 503 ' access.logwill work. To reproduce your problem I must do:
直接在 shell 中就行了grep '" 503 ' access.log。要重现您的问题,我必须执行以下操作:
bash -c 'grep '\" 503 ' access.log'
This is indeed syntax error. To make that work, I need:
这确实是语法错误。为了使这项工作,我需要:
bash -c 'grep "\" 503 " access.log'
You are somehow calling bash -c .... Maybe indirectly. You need to figure you how it's called to figure out what quotes are in collision.
你以某种方式调用bash -c .... 也许是间接的。您需要弄清楚它是如何调用的,以找出冲突中的引号。
回答by pyroscope
To debug strange effects like this, use "set -x" to show the shell expansions, and what the computer thinks about your command.
要调试这样的奇怪效果,请使用“set -x”来显示外壳扩展以及计算机对您的命令的看法。
回答by dtbarne
I believe I have it working now (not sure because I got no results, but didn't get an error).
我相信我现在可以使用它(不确定,因为我没有得到结果,但没有收到错误)。
The reason is because I'm passing it through an ssh command like the following and I believe SSH is doing some escape trickery:
原因是因为我通过如下所示的 ssh 命令传递它,并且我相信 SSH 正在做一些逃避技巧:
ssh 123.123.123.123 grep '" 503 ' access.log
Modifiying it to this seems to be the fix:
将其修改为这个似乎是解决方法:
ssh 123.123.123.123 "grep '\" 503 ' access.log"
Thanks for everyone's time.
感谢大家的时间。

