模板解析错误:模板::1:操作数中出现意外的“=”

huangapple go评论71阅读模式
英文:

template parsing error: template: :1: unexpected "=" in operand

问题

在Windows上执行以下命令时,我遇到了上述错误:

docker inspect --format="{{range $key, $value := .Config.Env}}{{if eq (index (split $value "=") 0) "VERSION"}}{{$value}}{{end}}{{end}}" octopusbi-agent-backend

可能的问题是模板语法错误。根据错误消息,模板中的等号("=")在此处不被期望。你可以尝试将等号替换为冒号(":"),并重新执行命令,看看问题是否解决。

英文:
template parsing error: template: :1: unexpected "=" in operand

I got the above error when executing the below command in Windows,

docker inspect --format="{{range $key, $value := .Config.Env}}{{if eq (index (split $value "=") 0) "VERSION"}}{{$value}}{{end}}{{end}}" octopusbi-agent-backend

What could be the issue?

答案1

得分: 0

"="符号的问题是,如果你在用双引号(")括起来的字符串中使用双引号("),你必须在每个双引号(")前面加上反斜杠(\),除了第一个和最后一个双引号(")。

例如:

"Hello "your_name""  <-- 错误
"Hello \"your_name\""  <-- 正确

<h1>Windows</h1>

如我之前提到的,我将"="改为\\"=\\",然后遇到了与另一个字符串值"VERSION"相关的问题。为了解决这个问题,我还必须将"VERSION"改为\\"VERSION\\",这样它就按照我预期的工作了。

模板解析错误:模板::1:操作数中出现意外的“=”

所以最终的命令是:

docker inspect --format="{{range $key, $value := .Config.Env}}{{if eq (index (split $value \"=\") 0) \"VERSION\"}}{{$value}}{{end}}{{end}}" octopusbi-agent-backend

<h1>Ubuntu</h1>

我在Ubuntu中运行了相同的命令,将起始和结束引号改为单引号('),其余部分保持双引号(")不变。

模板解析错误:模板::1:操作数中出现意外的“=”

所以最终的命令是:

docker inspect --format='{{range $key, $value := .Config.Env}}{{if eq (index (split $value "=") 0) "VERSION"}}{{$value}}{{end}}{{end}}' octopusbi-agent-backend

<h3>总结</h3>

如果你使用docker inspect命令和--format选项:

  • 在Windows中:
    1. 你必须用双引号(")标记开始格式字符串。
    2. 如果你想在格式字符串中使用双引号("),请使用\\"代替。
  • 在Ubuntu中:
    1. 你必须用单引号(')标记开始格式字符串。
    2. 可以在格式字符串中自由使用双引号(")。

简而言之,如果你需要使用引号,无论在哪个环境中,都必须在格式字符串中使用双引号(")。

英文:

The issue with the &quot;=&quot; symbol, if you use a double quotation(&quot;) mark inside the string enclosed with the double quotation(&quot;) marks, you have to add a backslash(\) before every double quotation(&quot;) mark excluding the first and last double quotation(&quot;) mark.

example:-

&quot;Hello &quot;your_name&quot;&quot;  &lt;-- wrong
&quot;Hello \&quot;your_name\&quot;&quot;  &lt;-- correct

<h1>Windows</h1>

As I mentioned earlier I changed &quot;=&quot; to \&quot;=\&quot; and after that, I got another issue related to some other string value called &quot;VERSION&quot;. For that also I had to change &quot;VERSION&quot; to \&quot;VERSION\&quot; and it worked as I expected.

模板解析错误:模板::1:操作数中出现意外的“=”

So the final command is,

docker inspect --format=&quot;{{range $key, $value := .Config.Env}}{{if eq (index (split $value \&quot;=\&quot;) 0) \&quot;VERSION\&quot;}}{{$value}}{{end}}{{end}}&quot; octopusbi-agent-backend

<h1>Ubuntu</h1>

I ran the same command in Ubuntu with the starting and ending quotations with single quotation(&#39;) marks and kept the rest of the double quotation(&quot;) marks.

模板解析错误:模板::1:操作数中出现意外的“=”

So the final command is,

docker inspect --format=&#39;{{range $key, $value := .Config.Env}}{{if eq (index (split $value &quot;=&quot;) 0) &quot;VERSION&quot;}}{{$value}}{{end}}{{end}}&#39; octopusbi-agent-backend

<h3>Summary</h3>

If you use the docker inspect command with the --format option,

  • In Windows:-
    1. You have to start the format string with double quotation(&quot;) marks.
    2. If you want to use a double quotation(&quot;) mark inside the format string, use \&quot; instead.
  • In Ubuntu:-
    1. You have to start the format string with single quotation(&#39;) marks.
    2. Feel free to use double quotation(&quot;) marks inside the format string.

In the shortest, we have to use the double quotation(&quot;) mark inside the format string for both environments if you need to use the quotation mark.

huangapple
  • 本文由 发表于 2023年4月21日 20:17:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76073083.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定