寻找匹配的“”时出现意外的 EOF;

发布于 2024-12-04 09:38:39 字数 361 浏览 1 评论 0原文

我正在编写一个脚本,其执行命令如下:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

万人眼中万个我 2024-12-11 09:38:39

那些逃脱是什么?

cat /abc | grep -v '^#' | grep -i root | sed -e '\''s/"//g'\'' | awk '\''{print $2}'\''
                                                 ^         ^          ^             ^

你的问题就在那里:

sed -e '\''s/"//g'\''
             ^ unmatched

what are those escapes ?!

cat /abc | grep -v '^#' | grep -i root | sed -e '\''s/"//g'\'' | awk '\''{print $2}'\''
                                                 ^         ^          ^             ^

Your problem is there:

sed -e '\''s/"//g'\''
             ^ unmatched
策马西风 2024-12-11 09:38:39

引用的都是错误的。为什么使用单引号、反斜杠、单引号、单引号,并且总是按这个顺序?无论如何,您有一个不带引号的双引号,因此 shell 希望您为以左双引号开头的带引号的字符串添加结束引号。

作为风格问题,您还应该放弃无用的 Cat,并考虑如何简化你的脚本。至少:

grep -v ^# /abc | grep -i root | sed -e 's/"//g' | awk '{print $2}'

...但在实践中

awk '/^#/ { next } /[Rr][Oo][Oo][Tt]/ { gsub ("\"",""); print $2 }' /abc

由于 awksed 脚本中的某些字符对 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:

grep -v ^# /abc | grep -i root | sed -e 's/"//g' | awk '{print $2}'

... but in practice

awk '/^#/ { next } /[Rr][Oo][Oo][Tt]/ { gsub ("\"",""); print $2 }' /abc

Because some of the characters in the awk and sed 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文