shell脚本:如何检查环境变量是否存在,并获取它的值?(复制)

时间:2022-06-01 20:36:04

This question already has an answer here:

这个问题已经有了答案:

I am writing a shell script. In this shell script, I am have a variable that either takes a default value, or the value of an environment variable. However, the environment variable doesn't have to be present.

我正在写一个shell脚本。在这个shell脚本中,我有一个变量,它要么取默认值,要么取环境变量的值。然而,环境变量并不一定存在。

For instance, assume, before running the script, I perform the following operation:

例如,假设在运行脚本之前,我执行以下操作:

export DEPLOY_ENV=dev

How do I tell the script to search for this environment variable, and store its value in a variable inside the script. Moreover, how do I tell the script that if this environment variable does not exist, store a default variable?

如何告诉脚本搜索这个环境变量,并将其值存储在脚本中的一个变量中。此外,如何告诉脚本,如果该环境变量不存在,则存储一个默认变量?

6 个解决方案

#1


93  

[ -z "${DEPLOY_ENV}" ] checks whether DEPLOY_ENV has length equal to zero. So you could run:

[-z "${DEPLOY_ENV}"]检查DEPLOY_ENV的长度是否为零。所以你可以运行:

if [[ -z "${DEPLOY_ENV}" ]]; then
  MY_SCRIPT_VARIABLE="Some default value because DEPLOY_ENV is undefined"
else
  MY_SCRIPT_VARIABLE="${DEPLOY_ENV}"
fi

# or using a short-hand version

[[ -z "${DEPLOY_ENV}" ]] && MyVar='default' || MyVar="${DEPLOY_ENV}"

# or even shorter use

MyVar="${DEPLOY_ENV:-default_value}"

#2


38  

You could just use parameter expansion:

你可以使用参数展开:

${parameter:-word}

$ {参数:词}

If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.

如果参数未设置或为空,则替换字的展开。否则,将替换参数的值。

So try this:

那么试试这个:

var=${DEPLOY_ENV:-default_value}

There's also the ${parameter-word} form, which substitutes the default value only when parameter is unset (but not when it's null).

还有${parameter-word}表单,它只在参数未设置时替换默认值(但在参数为null时不替换)。

To demonstrate the difference between the two:

证明两者的不同:

$ unset DEPLOY_ENV
$ echo "'${DEPLOY_ENV:-default_value}' '${DEPLOY_ENV-default_value}'"
'default_value' 'default_value'
$ DEPLOY_ENV=
$ echo "'${DEPLOY_ENV:-default_value}' '${DEPLOY_ENV-default_value}'"
'default_value' ''

#3


11  

If you don't care about the difference between an unset variable or a variable with an empty value, you can use the default-value parameter expansion:

如果您不关心未设置变量和空值变量之间的差异,可以使用default-value参数展开:

foo=${DEPLOY_ENV:-default}

If you do care about the difference, drop the colon

如果你真的关心它们的区别,那就去掉结肠

foo=${DEPLOY_ENV-default}

You can also use the -v operator to explicitly test if a parameter is set.

还可以使用-v操作符显式地测试是否设置了参数。

if [[ ! -v DEPLOY_ENV ]]; then
    echo "DEPLOY_ENV is not set"
elif [[ -z "$DEPLOY_ENV" ]]; then
    echo "DEPLOY_ENV is set to the empty string"
else
    echo "DEPLOY_ENV has the value: $DEPLOY_ENV"
fi

#4


3  

There is no difference between environment variables and variables in a script. Environment variables are just defined earlier, outside the script, before the script is called. From the script's point of view, a variable is a variable.

在脚本中,环境变量和变量之间没有区别。环境变量只是在调用脚本之前,在脚本之外定义的。从脚本的角度来看,变量是一个变量。

You can check if a variable is defined:

你可以检查是否定义了一个变量:

if [ -z "$a" ]
then
    echo "not defined"
else 
    echo "defined"
fi

and then set a default value for undefined variables or do something else.

然后为未定义的变量设置一个默认值或者做其他的事情。

The -z checks for a zero-length (i.e. empty) string. See man bash and look for the CONDITIONAL EXPRESSIONS section.

z检查零长度(即空)字符串。参见man bash并查找条件表达式部分。

You can also use set -u at the beginning of your script to make it fail once it encounters an undefined variable, if you want to avoid having an undefined variable breaking things in creative ways.

您还可以在脚本开始时使用set -u,使其在遇到未定义的变量时失败,如果您想避免使用未定义的变量以创造性的方式破坏事物。

#5


1  

NEW_VAR=""
if [[ ${ENV_VAR} && ${ENV_VAR-x} ]]; then
  NEW_VAR=${ENV_VAR}
else
  NEW_VAR="new value"
fi

#6


0  

All the answers worked. However, I had to add the variables that I needed to get to the sudoers files as follows:

所有的答案。但是,我必须添加我需要的变量来达到sudoers文件,如下所示:

sudo visudo
Defaults env_keep += "<var1>, <var2>, ..., <varn>"

#1


93  

[ -z "${DEPLOY_ENV}" ] checks whether DEPLOY_ENV has length equal to zero. So you could run:

[-z "${DEPLOY_ENV}"]检查DEPLOY_ENV的长度是否为零。所以你可以运行:

if [[ -z "${DEPLOY_ENV}" ]]; then
  MY_SCRIPT_VARIABLE="Some default value because DEPLOY_ENV is undefined"
else
  MY_SCRIPT_VARIABLE="${DEPLOY_ENV}"
fi

# or using a short-hand version

[[ -z "${DEPLOY_ENV}" ]] && MyVar='default' || MyVar="${DEPLOY_ENV}"

# or even shorter use

MyVar="${DEPLOY_ENV:-default_value}"

#2


38  

You could just use parameter expansion:

你可以使用参数展开:

${parameter:-word}

$ {参数:词}

If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.

如果参数未设置或为空,则替换字的展开。否则,将替换参数的值。

So try this:

那么试试这个:

var=${DEPLOY_ENV:-default_value}

There's also the ${parameter-word} form, which substitutes the default value only when parameter is unset (but not when it's null).

还有${parameter-word}表单,它只在参数未设置时替换默认值(但在参数为null时不替换)。

To demonstrate the difference between the two:

证明两者的不同:

$ unset DEPLOY_ENV
$ echo "'${DEPLOY_ENV:-default_value}' '${DEPLOY_ENV-default_value}'"
'default_value' 'default_value'
$ DEPLOY_ENV=
$ echo "'${DEPLOY_ENV:-default_value}' '${DEPLOY_ENV-default_value}'"
'default_value' ''

#3


11  

If you don't care about the difference between an unset variable or a variable with an empty value, you can use the default-value parameter expansion:

如果您不关心未设置变量和空值变量之间的差异,可以使用default-value参数展开:

foo=${DEPLOY_ENV:-default}

If you do care about the difference, drop the colon

如果你真的关心它们的区别,那就去掉结肠

foo=${DEPLOY_ENV-default}

You can also use the -v operator to explicitly test if a parameter is set.

还可以使用-v操作符显式地测试是否设置了参数。

if [[ ! -v DEPLOY_ENV ]]; then
    echo "DEPLOY_ENV is not set"
elif [[ -z "$DEPLOY_ENV" ]]; then
    echo "DEPLOY_ENV is set to the empty string"
else
    echo "DEPLOY_ENV has the value: $DEPLOY_ENV"
fi

#4


3  

There is no difference between environment variables and variables in a script. Environment variables are just defined earlier, outside the script, before the script is called. From the script's point of view, a variable is a variable.

在脚本中,环境变量和变量之间没有区别。环境变量只是在调用脚本之前,在脚本之外定义的。从脚本的角度来看,变量是一个变量。

You can check if a variable is defined:

你可以检查是否定义了一个变量:

if [ -z "$a" ]
then
    echo "not defined"
else 
    echo "defined"
fi

and then set a default value for undefined variables or do something else.

然后为未定义的变量设置一个默认值或者做其他的事情。

The -z checks for a zero-length (i.e. empty) string. See man bash and look for the CONDITIONAL EXPRESSIONS section.

z检查零长度(即空)字符串。参见man bash并查找条件表达式部分。

You can also use set -u at the beginning of your script to make it fail once it encounters an undefined variable, if you want to avoid having an undefined variable breaking things in creative ways.

您还可以在脚本开始时使用set -u,使其在遇到未定义的变量时失败,如果您想避免使用未定义的变量以创造性的方式破坏事物。

#5


1  

NEW_VAR=""
if [[ ${ENV_VAR} && ${ENV_VAR-x} ]]; then
  NEW_VAR=${ENV_VAR}
else
  NEW_VAR="new value"
fi

#6


0  

All the answers worked. However, I had to add the variables that I needed to get to the sudoers files as follows:

所有的答案。但是,我必须添加我需要的变量来达到sudoers文件,如下所示:

sudo visudo
Defaults env_keep += "<var1>, <var2>, ..., <varn>"