I want to evaluate a variable conditionally for use in my docker image build
RUN ARCHFLAG="$(uname -m)" && ARCH=$([ $ARCHFLAG == "aarch64" ] && echo "arm64" || echo $ARCHFLAG) && curl -sL "https://get.helm.sh/helm-v${HELM_VERSION}-linux-${ARCH}.tar.gz" | tar -xvz &&
mv linux-${ARCH}/helm /usr/bin/helm &&
chmod +x /usr/bin/helm &&
rm -rf linux-${ARCH}
I get the error
------
> [ 5/13] RUN ARCHFLAG="$(uname -m)" && ARCH=$([ ARCHFLAG == "aarch64"] && echo "arm64" || echo $ARCHFLAG) && curl -sL "https://get.helm.sh/helm-v3.6.3-linux-${ARCH}.tar.gz" | tar -xvz && mv linux-${ARCH}/helm /usr/bin/helm && chmod +x /usr/bin/helm && rm -rf linux-${ARCH}:
#8 0.248 /bin/sh: 1: [: aarch64: unexpected operator
#8 1.295
#8 1.295 gzip: stdin: not in gzip format
#8 1.296 tar: Child returned status 1
#8 1.296 tar: Error is not recoverable: exiting now
I’ve enclosing it as a string too, in the IDE the first $ARCHFLAG
shows up in the same colours as rest of the strings while the second $ARXCHFLAG
does show up as variable.
2
Answers
I don’t have aarch64 and arm64
However I tried your script using bash.exe at windows
Sample output:
I have used mv and rm commands using echo.
I used uname function to test your script.
Output obtained without using set -x and set +x commands.
Updated script:
You are using
[ $ARCHFLAG == "aarch64" ]
. This would work in bash, or in a "halfway POSIX shell" (for instance, a bash which is started in POSIX mode and implements the==
operator), but in a POSIX shell, you would get _ unexpected operator_. From the error message you posted, we see that you are not executing bash. I suggest that you either switch to bash, or write the test as[ "$ARCHFLAG" = "aarch64" ]
.