gpt4 book ai didi

docker - Dockerfile RUN命令损坏。如何正确逃脱?

转载 作者:行者123 更新时间:2023-12-02 20:43:21 25 4
gpt4 key购买 nike

使用ubuntu:latest docker 图片...

该命令在命令行上工作正常:

if [ "`echo hi there`" == "hi there" ]; then echo it worked; else echo nope; fi

root@9df9198ced39:/# if [ "`echo hi there`" == "hi there" ]; then echo it worked; else echo nope; fi
it worked

但是在Dockerfile RUN命令中,它没有:
FROM ubuntu
RUN if [ "`echo hi there`" == "hi there" ]; then echo it worked; else echo nope; fi

sh: 1: [: hi there: unexpected operator

我尝试转义 "backtick[]。我尝试使用 # escape=\ ...我尝试了JSON语法:
RUN ["sh", "-c", "if [ \"`echo hi there`\" == \"hi there\" ]; then echo it worked; else echo nope; fi"]

...似乎似乎找不到正确的方法。我要去哪里错了?

最佳答案

使用“=”而不是“==”

您得到不同结果的原因可能是由于不同的shell解释器。

$ bash
$ if [ "`echo hi there`" = "hi there" ]; then echo it worked; else echo nope; fi
it worked
$ if [ "`echo hi there`" == "hi there" ]; then echo it worked; else echo nope; fi
it worked

$ sh
$ if [ "`echo hi there`" = "hi there" ]; then echo it worked; else echo nope; fi
it worked
$ if [ "`echo hi there`" == "hi there" ]; then echo it worked; else echo nope; fi
sh: 2: [: hi there: unexpected operator

关于docker - Dockerfile RUN命令损坏。如何正确逃脱?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59594578/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com