加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

Windows命令行:为什么环境变量在&之后不可用

发布时间:2020-12-13 22:31:49 所属栏目:Windows 来源:网络整理
导读:看看以下命令:为什么在?之后不能立即获得值? C:set a=C:set a=3echo %a%%a%C:echo %a%3C:set a=3echo %a%3 但是,当我这样做 C:set a=C:set a=3set a = 3包含在列出的变量中! 我需要这个我在这里学到的技巧,获取命令的退出代码甚至输出管道: Windo
看看以下命令:为什么在&?之后不能立即获得值?

C:&;set a=

C:&;set a=3&echo %a%
%a%

C:&;echo %a%
3

C:&;set a=3&echo %a%
3

但是,当我这样做

C:&;set a=

C:&;set a=3&set

a = 3包含在列出的变量中!

我需要这个我在这里学到的技巧,获取命令的退出代码甚至输出管道:
Windows command interpreter: how to obtain exit code of first piped command
但我必须在make脚本中使用它,这就是为什么一切都必须在一行!
这就是我想要做的:

target:
    ($(command) & call echo %%^^errorlevel%% ^>$(exitcodefile)) 2>&1 | tee $(logfile) & set /p errorlevel_make=<$(exitcodefile) & exit /B %errorlevel_make%

但是errorlevel_make总是为空(带有退出代码的文件存在且包含正确的退出代码).

这是cmd中的错误吗?任何想法我能做什么?

解决方法

创建一个单独的批处理文件来解决这个问题要容易得多,因为即使对于专家来说也不是很明显.

但在你的情况下这应该工作

target:
    ($(command) & call echo %^^^^errorlevel% >$(exitcodefile)) 2>&1 | tee $(logfile) & set /p errorlevel_make=<$(exitcodefile) & call exit /B %^errorlevel_make%

单独的批次可能看起来像

extBatch.bat

@echo off
("%~1" & call echo %%^^errorlevel%% > "%~2") 2>&1 | tee "%~3" & set /p errorlevel_make=<"%~2" 
exit /B %errorlevel_make%

然后,您可以从make文件启动批处理

target:
    extBatch.bat $(command) $(exitcodefile) $(logfile)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读