批处理脚本失败的字符串比较

时间:2013-08-28 18:53:49

标签: windows batch-file string-comparison

调试“rpmbuild.bat”下面的脚本。 注意:它包含错误,但不完整。 这是命令行:

rpmbuild.bat -bb --target "noarch-pc-windows 7" --buildroot D:\MyPath\MyApp\buildroot --define "_topdir D:\MyPath\MyApp" MyApp.spec

这个想法是采用上面的“.bat”cmd参数,修改它们并重定向(通过cygwin)到具有相同名称的unix工具(rpmbuild)。所以有点像:

bash -c "rpmbuild -bb --target ""noarch-pc-windows 7"" --buildroot /cygdrive/d/MyPath/MyApp/buildroot --define ""_topdir /cygdrive/d/MyPath/MyApp"" MyApp.spec"

为了以正确的方式转换路径,有一个实用程序 cygpath

以下是 rpmbuild.bat 的来源。但它无法在行上使用字符串比较进行编译,因为建议here

SETLOCAL EnableExtensions EnableDelayedExpansion
PUSHD .

SET PARAM_COUNT = 0
FOR %%P IN (%*) DO (
    SET /A PARAM_COUNT += 1
    SET PARAMS[PARAM_COUNT] = %%P

    IF PARAM_COUNT GTR 1 IF PARAMS[PARAM_COUNT-1]=="--buildroot" (
        REM Update buildroot path with cygwin path
        FOR /F "tokens=*" %%i in ('cygpath %%P') do SET PARAMS[PARAM_COUNT]=%%i
    )

    REM string comparison for TOPDIR
    SET str1 = %%P
    IF NOT x%str1:_topdir=%==x%str1% (
        REM Update topdir path
        SET TOPDIR=%%P
        SET TOPDIR=%TOPDIR:~9,-1%
        FOR /F "tokens=*" %%i in ('cygpath "%TOPDIR%"') do SET NEW_TOPDIR=%%i
        SET PARAMS[PARAM_COUNT] = "_topdir %NEW_TOPDIR"
    )

    REM string comparison for .spec
    IF NOT x%str1:.spec=%==x%str1% (
        REM Replace path in spec-file
        SET OLD_PATH=%TOPDIR:\=\\%
        SET NEW_PATH=%NEW_TOPDIR:/=\/%

        sed -s -i -e s/%OLD_PATH%\\/%NEW_PATH%\//g %%P
    )
)

REM construct new rpmbuild command in cygwin

SET RPMBUILD_COMMAND = bash -c "rpmbuild
FOR /L %%i IN (1,1,PARAM_COUNT) DO SET RPMBUILD_COMMAND=!RPMBUILD_COMMAND! %%i
SET RPMBUILD_COMMAND=!RPMBUILD_COMMAND!"

REM Execute rpmbuild
%RPMBUILD_COMMAND

POPD
ENDLOCAL

如何解决?

1 个答案:

答案 0 :(得分:2)

  1. 此处为SET "str1 "=" %%P",两个不需要的空格
  2. 此处IF NOT x%str1:_topdir=%==x%str1%您位于需要for delayed expansion的{​​{1}}代码块中,此错误在您的脚本中更频繁发生。