开发者

Testing to see if an env variable is set in bash

In a bash script, I'm trying to test for the existence of a variable. But no matter what I do, my "if" test returns true. Here's the code:

ignored-deps-are-not-set () {
    if [ -z "${ignored_deps+x}" ]
    then
        return 0
    fi
    return 1
}

ignored_deps=1
ignored-deps-are-not-set
echo "ignored-deps function returns: $?"
if [ ignored-deps-are-not-set ]
then
    echo "variable is unset"
else
    echo "variable exists"
fi

Here is the output as written:

ignored-deps function returns: 1
variable is unset

An开发者_开发问答d the output when I comment out the line where ignored_deps is set.

ignored-deps function returns: 0
variable is unset

No matter what, it says the variable is unset. What am I missing?


This line:

if [ ignored-deps-are-not-set ]

tests whether the string 'ignored-deps-are-not-set' is empty or not. It returns true because the string is not empty. It does not execute a command (and hence the function).

If you want to test whether a variable is set, use one of the ${variable:xxxx} notations.

if [ ${ignored_deps+x} ]
then echo "ignored_deps is set ($ignored_deps)"
else echo "ignored_deps is not set"
fi

The ${ignored_deps+x} notation evaluates to x if $ignored_deps is set, even if it is set to an empty string. If you only want it set to a non-empty value, then use a colon too:

if [ ${ignored_deps:+x} ]
then echo "ignored_deps is set ($ignored_deps)"
else echo "ignored_deps is not set or is empty"
fi

If you want to execute the function (assuming the dashes work in the function name), then:

if ignored-deps-are-not-set
then echo "Function returned a zero (success) status"
else echo "Function returned a non-zero (failure) status"
fi


You're not actually executing the function:

if ignored-deps-are-not-set; then ...

Withing [] brackets, the literal string "ignored-deps-are-not-set" is seen as true.


if [ ${myvar:-notset} -eq "notset" ] then
   ...


Yet another way to test for the existence of a variable:

if compgen -A variable test_existence_of_var; then 
   echo yes
else 
   echo no
fi


--edit-- just realized that it's a function tha tyou're trying to call, convention is wrong.

See:

Z000DGQD@CND131D5W6 ~
$ function a-b-c() {
> return 1
> }

Z000DGQD@CND131D5W6 ~
$ a-b-c

Z000DGQD@CND131D5W6 ~
$ echo $?
1

Z000DGQD@CND131D5W6 ~
$ if a-b-c; then echo hi; else echo ho; fi
ho

Z000DGQD@CND131D5W6 ~
$ if [ a-b-c ]; then echo hi; else echo ho; fi
hi

Z000DGQD@CND131D5W6 ~

--edit end--

Fix the variable name (see my comment to your post)

then

See Parameter Expansion section in man bash.

${parameter:?word}:

Display Error if Null or Unset. If parameter is null or unset, the expansion of word (or a message to that effect if word is not present) is written to the standard error and the shell, if it is not interactive, exits. Otherwise, the value of parameter is substituted.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜