NSIS: How to check at compile time if an environment variable exists

StackOverflow https://stackoverflow.com/questions/22149007

  •  19-10-2022
  •  | 
  •  

Вопрос

!echo "$%MY_VAR%"

Prints out

$%MY_VAR% (...\installer.nsi:46)

So the variable isn't replaced at all. Is this a bug? I'm a bit stumped here. How am I supposed to test if the variable exists? So far I thought I could use

!if "$%MY_VAR%" = ""

but that's not going to work if there is no replacement at all. The logical conclusion would be to use this:

!if "$%MY_VAR%" = "$%MY_VAR%"

Am I doing it wrong?

Это было полезно?

Решение

!if x = y is actually a number test, you need to use == to test strings.

You need to trick the compiler to get this to work:

; NSIS 2+
!define DOLLAR $
!if "$%foo%" == "${DOLLAR}%foo%"
!echo "%foo% not set"
!endif

; NSIS 3+
!if "$%foo%" == "${U+24}%foo%"
!echo "%foo% not set"
!endif

Другие советы

I just did a quick test (NSIS 3.0a0 and Unicode NSIS 2.46.5, both on Windows 7 x64) and I have no problem getting the $%ENV_VAR% replaced. Here's how I'm testing it:

!if "$%MY_VAR%" != ""
    !echo "MY_VAR is set"
!else
    !warning "MY_VAR not set"
!endif

I take it you already double checked your environmental variable was really saved

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top