Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check the existence of an environment variable?

The documentation for if/ifdef is slightly confusing. For <?if [expression] ?>, it states:

  • Variables can be used to check for existence
    ...
  • If the variable doesn't exist, evaluation will fail and an error will be raised.

It turns out if you just go: <?if $(env.MY_VAR) ?> and MY_VAR is not defined, compilation will fail. How do I check for existence?

Ordinarily, this is where one would use an ifdef, but these work strangely in Wix too. Instead of using $(var.Variable) syntax, they use <?ifdef Variable?>, meaning environment variables can't be checked this way.

What do I need to do to get the equivalent of normal c pre-processor:

#ifdef MY_ENVIRONMENT_VARIABLE

in Wix?

like image 393
fredley Avatar asked Apr 17 '12 09:04

fredley


People also ask

How do I know my environment variable value?

To display the values of environment variables, use the printenv command. If you specify the Name parameter, the system only prints the value associated with the variable you requested.

How do I check if an environment variable exists in Python?

# Checking if an Environment Variable Exists in Python import os if 'USER' in os. environ: print('Environment variable exists! ') else: print('Environment variable does not exist. ') # Returns: # Environment variable exists!

How do I check if an environment variable is empty?

To find out if a bash variable is empty: Return true if a bash variable is unset or set to the empty string: if [ -z "$var" ]; Another option: [ -z "$var" ] && echo "Empty" Determine if a bash variable is empty: [[ ! -z "$var" ]] && echo "Not empty" || echo "Empty"


2 Answers

The correct way to reference environment variables in ifdef sections is:

<?ifdef env.MY_VAR?>
  ...
<?endif?>

This works as expected.

like image 106
fredley Avatar answered Oct 09 '22 11:10

fredley


<Condition Message="Missing Environment Variable Message Goes Here"><![CDATA[%envvargoeshere]]></Condition>

Put the above element in the Package element of the wxs file. The installation will fail at runtime (install time) with a nice message if the environment variable does not exist.

like image 40
wavydavy Avatar answered Oct 09 '22 13:10

wavydavy