简体   繁体   English

嵌套变量不在 Docker 的 bash 脚本中扩展

[英]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).在变量${download}上使用 curl 时,变量${tmlversion}不会与其一起传递,并且调试显示它为空白 (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.这个问题是在它被调用之前, ${tmlversion}实际上被定义为它应该是什么 (debug2) 但它仍然没有通过。

Snippet of the error from Docker:来自 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.用户运行带有-e VERSION=(tag)变量的容器。
  2. The script checks if its either blank, "latest" or a string of numbers (xxxx.xx.xx.xx).该脚本检查它是空白的、“最新的”还是一串数字 (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.根据变量现在是“最新”还是数字,它执行 2 条不同的 curl 行以从 github 适当地下载。

SOLUTION: I should have put the ${tmlversion} variable before the url variables:解决方案:我应该将${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

SOLUTION: (From Tripleee) I should have put the ${tmlversion} variable before the url variables:解决方案:(来自 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

Based solely on the code snippet provided, a bit of streamlining...完全基于提供的代码片段,有点精简......

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM