Linux 检查环境变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2981878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 20:06:31  来源:igfitidea点击:

Checking for environment variables

linuxbashshell

提问by Hamza

I am trying to check the value of an environment variable and depending on the value do certain things and it works fine as long as the variable is set. When it isn't though I get a whole bunch of errors (as BASH is trying to compare the string I specify with an undefined variable, I guess)

我正在尝试检查环境变量的值,并根据该值执行某些操作,只要设置了该变量,它就可以正常工作。当我没有收到一大堆错误时(因为 BASH 试图将我指定的字符串与未定义的变量进行比较,我猜)

I tried implementing an extra check to prevent it happening but no luck. The block of code I am using is:

我尝试实施额外的检查以防止它发生,但没有运气。我使用的代码块是:

#!/bin/bash

if [ -n $TESTVAR ]
then
  if [ $TESTVAR == "x" ]
  then
    echo "foo"
    exit
  elif [ $TESTVAR == "y" ]
  then
    echo "bar"
    exit
  else
    echo "baz"
    exit
  fi
else
  echo -e "TESTVAR not set\n"
fi

And this is the output:

这是输出:

$ export TESTVAR=x
$ ./testenv.sh 
foo
$ export TESTVAR=y
$ ./testenv.sh 
bar
$ export TESTVAR=q
$ ./testenv.sh 
baz
$ unset TESTVAR
$ ./testenv.sh 
./testenv.sh: line 5: [: ==: unary operator expected
./testenv.sh: line 9: [: ==: unary operator expected
baz

My question is, shouldn't 'unset TESTVAR' nullify it? It doesn't seem to be the case...

我的问题是,不应该“取消设置 TESTVAR”使其无效吗?好像不是这样...

采纳答案by Hamza

Enclose the variable in double-quotes.

用双引号将变量括起来。

if [ "$TESTVAR" = "foo" ]

if you do that and the variable is empty, the test expands to:

如果您这样做并且变量为空,则测试将扩展为:

if [ "" = "foo" ]

whereas if you don't quote it, it expands to:

而如果你不引用它,它会扩展为:

if [  = "foo" ]

which is a syntax error.

这是一个语法错误。

回答by msw

Look at the section titled "Parameter Expansion" you'll find things like:

查看标题为“参数扩展”的部分,您会发现以下内容:

${parameter:-word}
Use Default Values. If parameter is unset or null, the expan‐ sion of word is substituted. Otherwise, the value of parameter is substituted.

${parameter:-word}
使用默认值。如果参数未设置或为空,则替换单词的扩展。否则,替换参数的值。

回答by BillThor

After interpretation of the missing TESTVARyou are evaluating [ == "y" ]. Try any of:

后遗漏的解释TESTVAR你正在评估[ == "y" ]。尝试以下任何一种:

 "$TESTVAR"
 X$TESTVAR == Xy
 ${TESTVAR:-''}

回答by Paused until further notice.

In Bash (and ksh and zsh), if you use double square brackets you don't need to quote variables to protect against them being null or unset.

在 Bash(以及 ksh 和 zsh)中,如果使用双方括号,则不需要引用变量来防止它们为 null 或未设置。

$ if [ $xyzzy == "x" ]; then echo "True"; else echo "False"; fi
-bash: [: ==: unary operator expected
False
$ if [[ $xyzzy == "x" ]]; then echo "True"; else echo "False"; fi
False

There are other advantages.

还有其他优点

回答by intuited

Your test to see if the value is set

您的测试以查看该值是否已设置

if [ -n $TESTVAR ]

actually just tests to see if the value is set to something other than an empty string. Observe:

实际上只是测试以查看该值是否设置为空字符串以外的其他内容。观察:

$ unset asdf
$ [ -n $asdf ]; echo $?
0
$ [ -n "" ]; echo $?
1
$ [ -n "asdf" ]; echo $?
0

Remember that 0means True.

请记住,这0意味着 True。

If you don't need compatibility with the original Bourne shell, you can just change that initial comparison to

如果您不需要与原始 Bourne shell 兼容,您可以将初始比较更改为

if [[ $TESTVAR ]]