嵌套变量在Docker的bash脚本中未展开

ffscu2ro  于 2022-12-26  发布在  Docker
关注(0)|答案(2)|浏览(111)

当在变量${download}上使用curl时,变量${tmlversion}没有与它沿着传递,调试显示它是空的(debug3)。这个问题是在调用它之前,${tmlversion}实际上被定义为它应该是的样子(debug2),但它仍然没有传递。
来自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.
  ...

以下是我尝试过的代码片段:

#!/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)

以及

#!/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)

这在功能上应该是相同的,但它返回相同的错误。
下面是代码的概要:
1.用户使用-e VERSION=(tag)变量运行容器。
1.脚本检查它是空白、“最新”还是一串数字(xxxx.xx.xx.xx)。
1.为空时默认为“最新”
1.根据变量现在是“latest”还是数字,它执行2个不同的curl行,以便从github适当地下载。

**解决方案:**我应该把${tmlversion}变量放在url变量之前:

# $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
whhtz7ly

whhtz7ly1#

解决方案:(来自Tripleee)我应该把${tmlversion}变量放在url变量之前:

# $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
ie3xauqp

ie3xauqp2#

仅基于所提供的代码片段,进行一些精简...

#!/usr/bin/env bash

# $VERSION is passed down from Dockerfile. It is currently "2022.09.47.16"

echo "debug1 VERSION=${VERSION}"

if [ -z "${VERSION}" ]; then
    echo "|| Version tag was not specified. Defaulting to latest. ||"
    download="https://github.com/tModLoader/tModLoader/releases/latest/download/tModLoader.zip"
    tmlversion="latest"
else
    download="https://github.com/tModLoader/tModLoader/releases/download/v${VERSION}/tModLoader.zip"
    tmlversion="${VERSION}"
fi

echo "debug2 tmlversion=${tmlversion}"
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}"

    ... (anything past this is not important)

相关问题