简体   繁体   English

如何使用 Bash 遍历数组以检查列表环境变量?

[英]How to iterate over array to check list environment variables using Bash?

I have a bash script that runs a check to determine if the requisite environment variables are set before executing its logic.我有一个 bash 脚本,它运行检查以确定在执行其逻辑之前是否设置了必要的环境变量。

At the moment I'm repeating the following code block for every environment variable:目前,我正在为每个环境变量重复以下代码块:

if [[ -z "$SOME_ENV_VAR" ]]; then
    echo "Warning - the SOME_ENV_VAR environment variable isn't set"

    exit 1
fi

Is there a way to declare an array and iterate over the array and check if the environment variable is set?有没有办法声明一个数组并遍历数组并检查是否设置了环境变量?

I know how to iterate over arrays in bash, but I'm not sure how to use each iteration variable to check if a similarly named environment variable is present.我知道如何在 bash 中迭代 arrays,但我不确定如何使用每个迭代变量来检查是否存在类似命名的环境变量。

You can use indirection to access a variable by its name if that name is stored in another variable.如果该名称存储在另一个变量中,您可以使用间接方式通过变量名称访问该变量。 A small example:一个小例子:

x=1
name=x
echo "${!name}" # prints 1

With this approach, you only have to iterate over the names of the variables you want to check:使用这种方法,您只需遍历要检查的变量的名称:

for name in FIRST_ENV_VAR SECOND_ENV_VAR ...; do
    if [[ -z "${!name}" ]]; then
        echo "Variable $name not set!"
        exit 1
    fi
done

You may also want to use -z "${!name+x}" to accept variables that are set to the empty string, see here .您可能还想使用-z "${!name+x}"来接受设置为空字符串的变量,请参见此处

x=(
  LANG
  LC_MEASUREMENT
  LC_PAPER
  LC_MONETARY
  LC_NAME
  LC_ADDRESS
  LC_NUMERIC
  LC_TELEPHONE
  LC_IDENTIFICATION
  LC_TIME
)
for v in "${x[@]}"; do
  echo "$v = \"${!v}\""
done

Result:结果:

LANG = "en_US.UTF-8"
LC_MEASUREMENT = "en_US.UTF-8"
LC_PAPER = "en_US.UTF-8"
LC_MONETARY = "en_US.UTF-8"
LC_NAME = "en_US.UTF-8"
LC_ADDRESS = "en_US.UTF-8"
LC_NUMERIC = "en_US.UTF-8"
LC_TELEPHONE = "en_US.UTF-8"
LC_IDENTIFICATION = "en_US.UTF-8"
LC_TIME = "en_US.UTF-8"

See the bash (1) manual page for an explanation of indirect expansion using ${!请参阅bash (1) 手册页,了解使用${! name } .名称}

declare -a MY_ENVS

# Set the above array with whatever
# env variables you want to check
# e.g.,

MY_ENVS+=( "HOME" )

Loop through all of them,遍历所有这些,

for env_var in "${MY_ENVS[@]}"
do
    if [ -z "${env_var}" ]
    then
        echo "${env_var} not set"
    fi
done

Or in case you want to store values corresponding to particular env vars, use associative arrays:或者,如果您想存储与特定环境变量相对应的值,请使用关联 arrays:

declare -A ENV_VARS_A

ENV_VARS_A["HOME"]="some val"

Iteration:迭代:

for env_var in "${!ENV_VARS_A[@]}"
do
    # env_var is key
    :
done

You can create the list of the environment variables you want to iterate over as done in ENV_VAR_ARR .您可以创建要迭代的环境变量列表,如ENV_VAR_ARR中所做的那样。
Then iterate over each of them using a for loop.然后使用for循环遍历它们中的每一个。

The for loop will take each item in the list, assigns it SOME_ENV_VAR and execute the commands between do and done then go back to the top, grab the next item in the list and repeat over. for 循环将获取列表中的每个项目,为其分配SOME_ENV_VAR并执行 do 和 done 之间的命令,然后 go 回到顶部,获取列表中的下一个项目并重复。

The list is defined as a series of strings, separated by spaces.该列表定义为一系列字符串,以空格分隔。

#!/bin/bash

ENV_VAR_ARR='ENV_1 ENV_2 ENV_3'

for SOME_ENV_VAR in $ENV_VAR_ARR
do
    echo $SOME_ENV_VAR
    if [[ -z "${!SOME_ENV_VAR}" ]]; then
        echo "Warning - the $SOME_ENV_VAR environment variable isn't set"
        exit 1
    fi
done

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

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