这个脚本(open-switch/opx-build/scripts/opx_run)是如何传递变量的?

Gra*_*ell 4 bash shell-script docker

像许多 bash 问题一样,我 100% 肯定有答案,但在谷歌上搜索它们是具有挑战性的。

我试图理解以下脚本

#!/bin/bash

# available options
export OPX_GIT_TAG="${OPX_GIT_TAG:-no}"

# package distribution
export OPX_RELEASE="${OPX_RELEASE:-unstable}"
# currently tracked release
export DIST="${DIST:-stretch}"
export ARCH="${ARCH:-amd64}"

export CUSTOM_SOURCES="${CUSTOM_SOURCES:-}"

# docker image name
IMAGE="opxhub/build"
# docker image tag
VERSION="${VERSION:-latest}"

interactive="-i"
if [ -t 1 ]; then
  # STDOUT is attached to TTY
  interactive="-it"
fi

read -d '' opx_docker_command <<- EOF
docker run
  --rm
  --name ${USER}_$(basename $PWD)_$$
  --privileged
  -e LOCAL_UID=$(id -u ${USER})
  -e LOCAL_GID=$(id -g ${USER})
  -v ${PWD}:/mnt
  -v $HOME/.gitconfig:/home/opx/.gitconfig
  -v /etc/localtime:/etc/localtime:ro
  -e ARCH
  -e DIST
  -e OPX_RELEASE
  -e OPX_GIT_TAG
  -e CUSTOM_SOURCES
  ${interactive}
  ${IMAGE}:${VERSION}
EOF

if [[ $# -gt 0 ]]; then
  # run command directly
  # not using bash because tar fails to complete
  # root cause unknown (see opx_rel_pkgasm.py:tar_in)
  $opx_docker_command sh -l -c "$*"
else
  # launch interactive shell
  # using bash here because tar does not fail in an interactive shell
  $opx_docker_command bash -l
fi
Run Code Online (Sandbox Code Playgroud)

我被它们是如何产生的困惑docker-run有关部件-命令ARCHDISTOPX_RELEASE特别是-等。那些不应该用${}? 如果不是,这些变量是如何传递的?

Adm*_*Bee 5

如果仔细观察,您会发现这些变量的使用是在命令的一个-e选项中docker-run。此选项用于使容器可以使用环境变量。

所以在这里,脚本指定了应该传递给容器的环境变量的名称,而不是值本身(正如您正确所述,它需要取消引用,如$ARCH${ARCH})。

您可以查看docker 文档以进一步阅读。