使用ubuntu:latest
码头工人图片...
此命令在命令行上可以正常工作:
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"]
...似乎似乎找不到正确的方法。我要去哪里错了?
答案 0 :(得分:3)
使用'='而不是'=='
获得不同结果的原因可能是由于不同的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