寻找匹配的“”时出现意外的 EOF;
我正在编写一个脚本,其执行命令如下:
cat /abc | grep -v ^# | grep -i root | sed -e '\''s/"//g'\'' | awk '\''{print $2}'\''
在 SunOS 上运行脚本时,我收到以下错误:
test: line 1: unexpected EOF while looking for matching `"'
test: line 3: syntax error: unexpected end of file
尝试使用不同的选项..但没有运气。
需要有人帮助我确定上述命令中缺少什么。
I am writing a script which has command to execute as below:
cat /abc | grep -v ^# | grep -i root | sed -e '\''s/"//g'\'' | awk '\''{print $2}'\''
When running the script on SunOS, i am getting below error:
test: line 1: unexpected EOF while looking for matching `"'
test: line 3: syntax error: unexpected end of file
Tried with different option.. but no luck.
Need somebody help me identify what is missing in the above command.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那些逃脱是什么?
你的问题就在那里:
what are those escapes ?!
Your problem is there:
引用的都是错误的。为什么使用单引号、反斜杠、单引号、单引号,并且总是按这个顺序?无论如何,您有一个不带引号的双引号,因此 shell 希望您为以左双引号开头的带引号的字符串添加结束引号。
作为风格问题,您还应该放弃无用的 Cat,并考虑如何简化你的脚本。至少:
...但在实践中
由于
awk
和sed
脚本中的某些字符对 shell 具有特殊含义,因此我们将它们放在单引号中。如果脚本中需要使用单引号,则需要使用双引号;常见的模式是将单引号中的字符串与双引号中的字符串相邻,如下所示:echo '"'"'"
。这会回显"
(在单引号)紧接着'
(用双引号引起来)。编辑更新了引用问题的分析;添加了代码示例;更正的代码示例。最终编辑更正了 awk 脚本中对
gsub
的引用,并添加了对引用的小讨论。The quoting is all wrong. Why do you use single quote, backslash, single quote, single quote,and always in that order? Regardless, you have an unquoted double quote, so the shell expects you to add a closing quote for the quoted string which starts with that opening double quote.
As a matter of style, you should also lose the Useless Use of Cat, and think about how to simplify your script. At least:
... but in practice
Because some of the characters in the
awk
andsed
scripts have a special meaning to the shell, we put them in single quotes. If you need to have single quotes in a script, you need to double quote them; a frequent pattern is to have a string in single quotes adjacent to a string in double quotes, like this:echo '"'"'"
. This echos"
(quoted in single quotes) immediately followed by'
(quoted in double quotes).Edit Updated analysis of quoting problem; added code example; corrected code example. Final edit corrects quoting of
gsub
in awk script, and adds a small discussion of quoting.