检查的环境变量而没有命令扩展,不使用批处理文件中定义的? [英] Check if an environment variable is defined without command extensions and without using a batch file?

查看:143
本文介绍了检查的环境变量而没有命令扩展,不使用批处理文件中定义的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用CMD.EXE命令行(CMD.EXE正在从石膏构建工具的称呼)来确定一个环境变量是否定义或没有。我怎样才能做到这一点?我还好假设变量的值不包含单或双引号,但不能假定启用了命令扩展。

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.

我试过以下,这在.bat文件的伟大工程,但在命令行上直接输入失败:

I've tried the following, which works great in a .bat file, but fails when typed directly on the command line:

IF "%UNDEFINED%" == "" (echo yes)

在这确切的行是一个.bat文件并执行,我看作为输出。当我在命令行中键入它,输出是空的。我在Windows XP SP3测试这一点,虽然我的同事看到在Windows 7中相同的结果这是 HTTP建议的方法:// support.microsoft.com/kb/121170 和<一个href=\"http://www.robvanderwoude.com/battech_defined.php\">http://www.robvanderwoude.com/battech_defined.php.我不想使用如果定义未定义(回声是),因为如果命令扩展被禁用,将无法正常工作。

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/121170 and 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.

在下面的帖子中得票最多的回答使我相信,这个问题涉及到如何%的膨胀在CmdLineParser不同的处理主场迎战BatchLineParser,但仍然没有导致我一个解:
<一href=\"http://stackoverflow.com/questions/4094699/how-does-the-windows-command-inter$p$pter-cmd-exe-parse-scripts\">How不Windows命令国米preTER(CMD.EXE)解析脚本?

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?

推荐答案

OK,这花了一点,但我想我已经想通了。试试这个:

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.

这适用于所有AFAIK情况下,不依赖于任何命令扩展功能。

This works for all cases AFAIK, and does not rely on any command extension features.

这篇关于检查的环境变量而没有命令扩展,不使用批处理文件中定义的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆