解析输出时如何纠正变量覆盖不当行为? [英] How to correct variable overwriting misbehavior when parsing output?

查看:27
本文介绍了解析输出时如何纠正变量覆盖不当行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码检查批处理文件中的基板信息.

BaseboardCheck.cmd:

@echo offsetlocal EnableDelayedExpansion对于/f "tokens=1,2* delims==";%%a in ('wmic baseboard get/format:list') do (如果 [%%a"] EQU [产品"] (设置平台信息=%%b如果定义了 PlatformInfo (回声.!平台信息!echo.!PlatformInfo!这会覆盖变量))if [%%a"] EQU [版本"] (设置底板版本=%%b如果定义了 BaseboardVersion (回声.!底板版本!echo.!BaseboardVersion!这会覆盖变量)))

上述问题:当echo'd out时,变量被覆盖而不是附加到.

输出:

DX79SI这会覆盖变量AAG28808-600这会覆盖变量

我想得到的是:

DX79SIDX79SI这会覆盖变量AAG28808-600AAG28808-600这会覆盖变量

我已经在这个问题上花费了几个小时(并将继续这样做),但我希望其他人遇到了这个问题.我希望遇到这个解析问题的其他人将来可以避免它.

由此产生的另一个问题是它似乎破坏了条件逻辑.

更新:

经过所有的帮助,我想出了这个解决方案:

for/f skip=2 tokens=1,2 delims="%%a in ('wmic baseboard get Product^,Version^,Width/format:csv') do (设置平台=%%a设置底板版本=%%b)echo.Platform: %Platform%echo.Version %BaseboardVersion%

解决方案

是的,你有问题,但不是你想的那样.

wmic 有一个特殊的行为:在每一行输出的末尾有一个附加的回车,即每一行都以 0x0d 0x0d 0x0a

这个额外的回车存储在你的变量中,当回显到控制台时,你会得到数据和回车,所以,如果变量后面跟着更多的文本,因为光标已经定位在行(回车),这段文字在前面回显的数据上回显.

怎么解决?

@echo off设置本地启用延迟扩展for/f "tokens=1,* delims==" %%a in ('wmic baseboard get/format:list') DO (if ["%%a"] EQU ["产品"] (对于/f "delims=" %%c in ("%%b") 设置 "PlatformInfo=%%c"如果定义了 PlatformInfo (回声(!平台信息!echo(!PlatformInfo!This 不会覆盖变量))if ["%%a"] EQU ["版本"] (对于/f "delims=" %%c in ("%%b") 设置 "BaseboardVersion=%%c"如果定义了 BaseboardVersion (回声(!底板版本!echo(!BaseboardVersion!This 不会覆盖变量)))

在这种情况下,无需更改您的代码逻辑,可以使用附加的 for/f 命令来删除附加的回车

I am checking for baseboard information in a batch file with the following code.

BaseboardCheck.cmd:

@echo off
setlocal EnableDelayedExpansion

for /f "tokens=1,2* delims==" %%a in ('wmic baseboard get /format:list') do ( 
    
    if ["%%a"] EQU ["Product"] (
        set PlatformInfo=%%b

        if defined PlatformInfo (
            echo.!PlatformInfo!
            echo.!PlatformInfo!This overwrites the variable
        )
    )

    if ["%%a"] EQU ["Version"] (
        set BaseboardVersion=%%b

        if defined BaseboardVersion (
            echo.!BaseboardVersion!
            echo.!BaseboardVersion!This overwrites the variable
        )
    )   
)

The above problem: The variables get overwritten rather than appended to when echo'd out.

Output:

DX79SI
This overwrites the variable
AAG28808-600
This overwrites the variable

What I'd like to get is:

DX79SI
DX79SIThis overwrites the variable
AAG28808-600
AAG28808-600This overwrites the variable

I have spent a few hours on this (and will continue to do so), but I am hoping someone else has run into this issue. And I am hoping anyone else who runs into this parsing problem can avoid it in the future.

The additional problem that comes out of this is that it seems to break conditional logic.

Update:

After all of the assistance, I came up with this solution:

for /f "skip=2 tokens=1,2 delims=," %%a in ('wmic baseboard get Product^,Version^,Width /format:csv') do (
    set Platform=%%a
    set BaseboardVersion=%%b
)
echo.Platform: %Platform% 
echo.Version %BaseboardVersion%

解决方案

Yes, you have a problem, but it is not what you think.

wmic has a particular behaviour: at the end of each line output there is an aditional carriage return, that is, each line ends with 0x0d 0x0d 0x0a

This aditional carriage return is stored inside your variable and, when echoed to console, you get the data and the carriage return, so, if the variable is followed by more text, as the cursor has been positioned at the start of the line (the carriage return), this text is echoed over the previous echoed data.

How to solve?

@echo off
setlocal enabledelayedexpansion

for /f "tokens=1,* delims==" %%a in ('wmic baseboard get /format:list') DO ( 


    if ["%%a"] EQU ["Product"] (
        for /f "delims=" %%c in ("%%b") do set "PlatformInfo=%%c"

        if defined PlatformInfo (
            echo(!PlatformInfo!
            echo(!PlatformInfo!This does not overwrite the variable
        )
    )

    if ["%%a"] EQU ["Version"] (
        for /f "delims=" %%c in ("%%b") do set "BaseboardVersion=%%c"

        if defined BaseboardVersion (
            echo(!BaseboardVersion!
            echo(!BaseboardVersion!This does not overwrite the variable
        )
    )   
)

In this case, without changing your code logic, an aditional for /f command can be used to remove the aditional carriage return

这篇关于解析输出时如何纠正变量覆盖不当行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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