【问题标题】:AWK: error with variableAWK:变量错误
【发布时间】:2013-03-20 07:18:01
【问题描述】:

我正在编写一个 bash 脚本,并且有一些我想传递给 awk 调用的变量。终端响应消息“awk: line 1: syntax error at or near

这是失败的变量:

helpInsert="\
\t<help name=\"vsss.status\"><![CDATA[Displays the active configuration.]]></help>\n\
\t<help name=\"vsss.general\"><![CDATA[Allows configuration of general video server parameters. <br />\n\
\t\tPressing restore defaults will reset the working configuration, but will not affect the active configuration. <br />\n\
\t\tPressing activate will make your working configuration into the active configuration. ]]></help>\n\
\t<help name=\"vsss.conf2\"><![CDATA[Allows editing of the working configuration. <br />\n\
\t\tSettings are not saved unless submit button is pressed]]></help>\
"

这是一个通过的变量样本,其余类似:

menuInsert="\
\t<subpageLvl1>\n\
\t\t<name>VSSS</name>\n\
\t\t<php></php>\n\
\t\t<login>0</login>\n\
\t\t<subpagesLvl2>\n\
\t\t\t<subpageLvl2>\n\
\t\t\t\t<name>Status</name>\n\
\t\t\t\t<php>vsss.status</php>\n\
\t\t\t\t<xml>vsss.xml</xml>\n\
\t\t\t\t<xsd>vsss.xsd</xsd>\n\
\t\t\t\t<login>0</login>\n\
\t\t\t</subpageLvl2>\n\
\t</subpageLvl1>\
"

这是电话:

awk -v appName=$appName -v insert=$helpInsert 'awk script here' filename

我知道问题出在变量而不是 awk 脚本上。我可能在某个地方错过了逃生机会,但我似乎找不到。谁能帮我解决这个问题?

【问题讨论】:

  • 使用引号:-v insert="$helpInsert"
  • @WilliamPursell Geez 我知道这很简单。你能把这个作为答案,所以我可以说这个问题已经回答了

标签: bash variables awk


【解决方案1】:

你需要使用双引号来保护变量不被shell扩展:

awk -v appName="$appName" -v insert="$helpInsert" 'awk script here' filename

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-30
    • 2017-08-25
    • 1970-01-01
    • 2019-01-17
    • 2019-03-18
    • 2012-02-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多