Search code examples
bashdockervariables

Nested variables not expanding in a bash script in Docker


When using curl on the variable ${download}, the variable ${tmlversion} isn't being passed along with it and debug shows that it is blank (debug3). This issue is that right before its being called, the ${tmlversion} is actually defined as what it should be (debug2) but it still isnt passed through.

Snippet of the error from Docker:

debug1 VERSION=2022.09.47.16
debug2 tmlversion=2022.09.47.16
|| TML version is 2022.09.47.16. ||
debug3 download=https://github.com/tModLoader/tModLoader/releases/download/v/tModLoader.zip
|| No server files were detected. Proceeding to download server files. ||
|| Downloading version 2022.09.47.16. ||
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100     9    0     9    0     0     50      0 --:--:-- --:--:-- --:--:--    51
  End-of-central-directory signature not found.  Either this file is not
  a zipfile, or it constitutes one disk of a multi-part archive.  In the
  latter case the central directory and zipfile comment will be found on
  the last disk(s) of this archive.
unzip:  cannot find zipfile directory in one of /server/tModLoader.zip or
        /server/tModLoader.zip.zip, and cannot find /server/tModLoader.zip.ZIP, period.
  ...

Here are snippets of code I've tried:

#!/usr/bin/env bash

# $VERSION is passed down from Dockerfile. It is currently "2022.09.47.16"
tmlversionlatest=https://github.com/tModLoader/tModLoader/releases/latest/download/tModLoader.zip
tmlversionspecific=https://github.com/tModLoader/tModLoader/releases/download/v${tmlversion}/tModLoader.zip

echo "debug1 VERSION=${VERSION}"
if [ -z "${VERSION}" ]; then
  echo "|| Version tag was not specified. Defaulting to latest. ||"
  tmlversion=latest
else
  tmlversion=${VERSION}
fi
echo "debug2 tmlversion=${tmlversion}"

echo "|| TML version is ${tmlversion}. ||"
if [ "${tmlversion}" = "latest" ]; then
  download=${tmlversionlatest}
else
  download=${tmlversionspecific}
fi
echo "debug3 download=${download}"

cd /server/
if [ ! -d "/server/Libraries/" ]; then
  echo "|| No server files were detected. Proceeding to download server files. ||"
  echo "|| Downloading version ${tmlversion}. ||"
  curl -LO ${download}
  unzip -o /server/tModLoader.zip
  rm -r /server/tModLoader.zip
  ... (anything past this is not important)

and

#!/usr/bin/env bash

# $VERSION is passed down from Dockerfile. It is currently "2022.09.47.16"
tmlversionlatest=https://github.com/tModLoader/tModLoader/releases/latest/download/tModLoader.zip
tmlversionspecific=https://github.com/tModLoader/tModLoader/releases/download/v${tmlversion}/tModLoader.zip

tmlversion=${VERSION}

echo "|| TML version is ${tmlversion}. ||"
if [ "${tmlversion}" = "latest" ]; then
  download=${tmlversionlatest}
else
  if [ -z "${tmlversion}" ]; then
    echo "|| Version tag was not specified. Defaulting to latest. ||"
    download=${tmlversionlatest}
  else
    echo "debug2 tmlversion=${tmlversion}"
    download=${tmlversionspecific}
  fi
fi
echo "debug3 download=${download}"

cd /server/
if [ ! -d "/server/Libraries/" ]; then
  echo "|| No server files were detected. Proceeding to download server files. ||"
  echo "|| Downloading version ${tmlversion}. ||"
  curl -LO ${download}
  unzip -o /server/tModLoader.zip
  rm -r /server/tModLoader.zip
  ... (anything past this is not important)

Which should be functionally the same but it returns the same error.

Here's the run down on the code:

  1. A user runs a container with a -e VERSION=(tag) variable.
  2. The script checks if its either blank, "latest" or a string of numbers (xxxx.xx.xx.xx).
  3. If it is blank, it defaults to "latest"
  4. Depending on if the variable is now "latest" or the numbers, it executes 2 different curl lines to appropriately download from github.

SOLUTION: I should have put the ${tmlversion} variable before the url variables:

# $VERSION is passed down from Dockerfile
tmlversion=${VERSION}
tmlurllatest=https://github.com/tModLoader/tModLoader/releases/latest/download/tModLoader.zip
tmlurlspecific=https://github.com/tModLoader/tModLoader/releases/download/v${tmlversion}/tModLoader.zip

Solution

  • SOLUTION: (From Tripleee) I should have put the ${tmlversion} variable before the url variables:

    # $VERSION is passed down from Dockerfile
    tmlversion=${VERSION}
    tmlurllatest=https://github.com/tModLoader/tModLoader/releases/latest/download/tModLoader.zip
    tmlurlspecific=https://github.com/tModLoader/tModLoader/releases/download/v${tmlversion}/tModLoader.zip