简体   繁体   English

Bash:如何检查是否设置了动态环境变量?

[英]Bash: How to check if dynamic env variable is set?

I'm writing a script where I want to dynamically create an environment variable name, and check if it has been set. 我正在编写一个脚本,要在其中动态创建一个环境变量名称,并检查它是否已设置。

#!/bin/bash

#######################################
# Builds options string
# Globals:
#   JVM_OPTS_DIR
# Arguments:
#   1. Options env variable name
#   2. File containing options defaults
# Returns:
#   Options
#######################################
function buildOpts() {
  declare -n opts=$1
  declare -n excludeOpts="EXCLUDE_$1"
  local -r optsFile=$2
  local x=
  if [ -z ${excludeOpts+x} ]; then
    while read -r o; do
      if [ -n "${o// }" ]; then
        x+=" $o"
      fi
    done <"$JVM_OPTS_DIR/$optsFile"
  fi
  if [ -n "$opts" ]; then
    for o in $opts; do
      x+=" $o"
    done
  fi
  printf '%s' "$x"
}

# https://docs.oracle.com/javase/8/docs/technotes/tools/unix/java.html

# Standard options
stdOpts=$(buildOpts STD_OPTS std.opts)

...

javaCmd="java $stdOpts $nonStdOpts $advRtOpts $advCompOpts $advServOpts $advGcOpts -jar $APP_DIR/app.jar"

printf '%s' "$javaCmd"

# eval $javaCmd "$@"

The above script serves as a Docker entrypoint 上面的脚本用作Docker入口点

docker build -t jdk . && docker run --rm -it jdk -e APP_NAME=test -e APP_LOG_DIR=test -e APP_DIR=test -e STD_OPTS='a b' -e EXCLUDE_STD_OPTS=true

However, I don't see a and b included in the javaCmd , neither do I see the EXCLUDE working. 但是,我没有看到javaCmd包含ab ,也没有看到EXCLUDE工作。 Basically, none of the if conditions in function buildOpts are working. 基本上,函数buildOpts中的if条件都不起作用。

I'm a backend programmer, and not a Bash wizard. 我是后端程序员,而不是Bash向导。 Help. 救命。

You need to put the envs flags before the image name. 您需要在图像名称之前放置envs标志。

Syntax of docker run is: docker run语法是:

docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]

Anything after the image name will be treated as [COMMAND][ARG...] 图片名称后的任何内容都将被视为[COMMAND][ARG...]

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

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