英文:
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\\"
,这样它就按照我预期的工作了。
所以最终的命令是:
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中运行了相同的命令,将起始和结束引号改为单引号('
),其余部分保持双引号("
)不变。
所以最终的命令是:
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中:
- 你必须用双引号(
"
)标记开始格式字符串。 - 如果你想在格式字符串中使用双引号(
"
),请使用\\"
代替。
- 你必须用双引号(
- 在Ubuntu中:
- 你必须用单引号(
'
)标记开始格式字符串。 - 可以在格式字符串中自由使用双引号(
"
)。
- 你必须用单引号(
简而言之,如果你需要使用引号,无论在哪个环境中,都必须在格式字符串中使用双引号("
)。
英文:
The issue with the "="
symbol, if you use a double quotation("
) mark inside the string enclosed with the double quotation("
) marks, you have to add a backslash(\
) before every double quotation("
) mark excluding the first and last double quotation("
) mark.
example:-
"Hello "your_name"" <-- wrong
"Hello \"your_name\"" <-- correct
<h1>Windows</h1>
As I mentioned earlier I changed "="
to \"=\"
and after that, I got another issue related to some other string value called "VERSION"
. For that also I had to change "VERSION"
to \"VERSION\"
and it worked as I expected.
So the final command is,
docker inspect --format="{{range $key, $value := .Config.Env}}{{if eq (index (split $value \"=\") 0) \"VERSION\"}}{{$value}}{{end}}{{end}}" octopusbi-agent-backend
<h1>Ubuntu</h1>
I ran the same command in Ubuntu with the starting and ending quotations with single quotation('
) marks and kept the rest of the double quotation("
) marks.
So the final command is,
docker inspect --format='{{range $key, $value := .Config.Env}}{{if eq (index (split $value "=") 0) "VERSION"}}{{$value}}{{end}}{{end}}' octopusbi-agent-backend
<h3>Summary</h3>
If you use the docker inspect
command with the --format
option,
- In Windows:-
- You have to start the format string with double quotation(
"
) marks. - If you want to use a double quotation(
"
) mark inside the format string, use\"
instead.
- You have to start the format string with double quotation(
- In Ubuntu:-
- You have to start the format string with single quotation(
'
) marks. - Feel free to use double quotation(
"
) marks inside the format string.
- You have to start the format string with single quotation(
In the shortest, we have to use the double quotation("
) mark inside the format string for both environments if you need to use the quotation mark.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论