Home > Back-end >  how to use a shell variable inside another eval block
how to use a shell variable inside another eval block

Time:08-31

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.

CodePudding user response:

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" ].

CodePudding user response:

I don't have aarch64 and arm64 However I tried your script using bash.exe at windows

#!/bin/bash
set -x
unset -f uname
uname ()
{
        echo -n aarch64
}
HELM_VERSION=3.9.2
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 && \
echo mv linux-${ARCH}/helm /usr/bin/helm && \
echo chmod  x /usr/bin/helm && \
echo rm -rf linux-${ARCH}
set  x

Sample output:

$ ./73542210.sh
-bash 13 unset -f uname
-bash 13 HELM_VERSION=3.9.2
--bash 13 uname -m
--bash 13 echo -n aarch64
-bash 13 ARCHFLAG=aarch64
--bash 13 '[' aarch64 == aarch64 ']'
--bash 13 echo arm64
-bash 13 ARCH=arm64
-bash 13 curl -sL https://get.helm.sh/helm-v3.9.2-linux-arm64.tar.gz
-bash 13 tar -xvz
linux-arm64/
linux-arm64/helm
linux-arm64/LICENSE
linux-arm64/README.md
-bash 13 echo mv linux-arm64/helm /usr/bin/helm
mv linux-arm64/helm /usr/bin/helm
-bash 13 echo chmod  x /usr/bin/helm
chmod  x /usr/bin/helm
-bash 13 echo rm -rf linux-arm64
rm -rf linux-arm64
-bash 13 set  x
$ ls -ltr linux-arm64/helm
-rwxr-xr-x 1 murugesan openssl 45875200 Jul 21 23:11 linux-arm64/helm
$ ls -ltr linux-arm64
total 44816
-rwxr-xr-x 1 murugesan openssl 45875200 Jul 21 23:11 helm
-rw-r--r-- 1 murugesan openssl     3367 Jul 21 23:21 README.md
-rw-r--r-- 1 murugesan openssl    11373 Jul 21 23:21 LICENSE

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:

$ rm -rf linux-arm64
$ ./73542210.sh
linux-arm64/
linux-arm64/helm
linux-arm64/LICENSE
linux-arm64/README.md
mv linux-arm64/helm /usr/bin/helm
chmod  x /usr/bin/helm
rm -rf linux-arm64
$ ./73542210.sh
ls -ltr linux-arm64
total 44816
-rwx---r-x  1 murugesan openssl 45875200 Jul 21 23:11 helm
-rw----r--  1 murugesan openssl     3367 Jul 21 23:21 README.md
-rw----r--  1 murugesan openssl    11373 Jul 21 23:21 LICENSE
  • Related