windows 检查环境变量是否在没有命令扩展名且没有使用批处理文件的情况下定义?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16367814/
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
Check if an environment variable is defined without command extensions and without using a batch file?
提问by Johann
I need to use a cmd.exe command line (cmd.exe is being called from the gyp build tool) to determine whether an environment variable is defined or not. How can I do this? I am okay assuming that the variable value does not contain single or double quotes, but cannot assume that command extensions are enabled.
我需要使用 cmd.exe 命令行(从 gyp 构建工具调用 cmd.exe)来确定是否定义了环境变量。我怎样才能做到这一点?我可以假设变量值不包含单引号或双引号,但不能假设启用了命令扩展。
I've tried the following, which works great in a .bat file, but fails when typed directly on the command line:
我尝试了以下方法,它们在 .bat 文件中效果很好,但在命令行上直接键入时失败:
IF "%UNDEFINED%" == "" (echo yes)
When that exact line is in a .bat file and executed, I see yes
as the output. When I type it on the command line, the output is empty. I am testing this on Windows XP SP3, though my coworker sees the same results on Windows 7. This is the method suggested by http://support.microsoft.com/kb/121170and http://www.robvanderwoude.com/battech_defined.php. I do not want to use IF DEFINED UNDEFINED (echo yes)
because that won't work if command extensions are disabled.
当该行在 .bat 文件中并执行时,我将其yes
视为输出。当我在命令行上输入时,输出为空。我正在 Windows XP SP3 上对此进行测试,尽管我的同事在 Windows 7 上看到了相同的结果。这是http://support.microsoft.com/kb/121170和http://www.robvanderwoude.com/建议的方法battech_defined.php。我不想使用,IF DEFINED UNDEFINED (echo yes)
因为如果禁用命令扩展,那将不起作用。
The top-voted answer in the following post has led me to believe that this issue is related to how percent-expansion is handled differently in the "CmdLineParser" vs. the "BatchLineParser," but still has not led me to a solution: How does the Windows Command Interpreter (CMD.EXE) parse scripts?
以下帖子中最高投票的答案让我相信这个问题与“CmdLineParser”与“BatchLineParser”中百分比扩展的处理方式有关,但仍然没有让我找到解决方案: How Windows 命令解释器 (CMD.EXE) 解析脚本吗?
采纳答案by RBarryYoung
OK, this took a bit, but I think I've figured it out. Try this:
好的,这花了一些时间,但我想我已经弄清楚了。尝试这个:
SET UNDEFINED 2>Nul | Findstr/I "."
IF ERRORLEVEL 1 ECHO Not Defined.
This works for all cases AFAIK, and does not rely on any command extension features.
这适用于所有情况 AFAIK,并且不依赖于任何命令扩展功能。
回答by Simon Catlin
Errr... just:
呃……只是:
if defined your-var-name (
echo yarp
) else (
echo narp
)
I should add, I do not believe this needs command extensions...
我应该补充一点,我认为这不需要命令扩展......
回答by jeb
If the extensions are really disabled (I can't believe this),
then you can try different ways.
如果扩展程序真的被禁用(我不敢相信),
那么您可以尝试不同的方法。
IF %UNDEFINED% == %^UNDEFINED% (echo yes)
This works as if undefined
doesn't exists then it isn't replaced, also ^undefined
but the caret will be removed in the next parser phase, so %undefined%
is compared against %undefined%
.
The disadvantage are the missing quotes, as they also make the expression stable against special characters.
这就像undefined
不存在一样工作,然后它不会被替换,^undefined
但插入符号将在下一个解析器阶段被删除,因此%undefined%
与%undefined%
. 缺点是缺少引号,因为它们也使表达式对特殊字符稳定。
A better way is to use IF defined
, but when extensions are disabled you need to enable them first.
更好的方法是使用IF defined
,但是当扩展被禁用时,您需要先启用它们。
cmd /E:on /c "if not defined undefined echo It's undefined"
The best way is to simply use a batch file, that should also work with gyp build system.
最好的方法是简单地使用批处理文件,它也应该与 gyp 构建系统一起使用。
回答by James L.
I tried this and it worked:
我试过了,它奏效了:
@echo off
setlocal disableextensions
set x=%path%
if "%x%"=="" (echo "PATH" does not exist) else (echo "PATH" exists)
set x=%pathx%
if "%x%"=="" (echo "PATHX" does not exist) else (echo "PATHX" exists)
endlocal
It returned:
它返回:
"PATH" exists
"PATHX" does not exist
回答by Lostgallifreyan
IF NOT %CODE%==? do stuff.
This works on a W98 command line and in a batch file, so it ought to work anywhere from early MS-DOS onwards with no extensions needed. It assumes that CODE is usefully set or not set at all.
这适用于 W98 命令行和批处理文件,因此它应该可以从早期的 MS-DOS 开始工作,无需扩展。它假定 CODE 被有效设置或根本没有设置。
It results in a syntax error if CODE does not exist, or does nothing if CODE is a question mark (chosen because it could never exist in a path). Either way, nothing is done. The NOT makes sure action is only taken if CODE appears to be set to something useful.
如果 CODE 不存在,则会导致语法错误,或者如果 CODE 是问号(选择它是因为它永远不会存在于路径中)则不执行任何操作。无论哪种方式,什么都没有做。NOT 确保仅当 CODE 似乎被设置为有用的东西时才采取行动。
I use it in compiler batch files to determine whether to use relative paths or use a fixed base directory if one is set in the CODE variable. It allows me to make a coding tree portable without having to modify all the batch files for each program if I move everything.
我在编译器批处理文件中使用它来确定是使用相对路径还是使用固定基目录(如果在 CODE 变量中设置)。如果我移动所有内容,它允许我使编码树可移植,而无需修改每个程序的所有批处理文件。