Alec
Alec

Reputation: 1496

Batch scripting failed string comparison

Debugging the script below "rpmbuild.bat". NOTE: it contains bugs, not complete. This is command line:

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

The idea is take the above ".bat" cmd parameters, modify them and redirect (via cygwin) to unix tool with the same name (rpmbuild). So to have smth like:

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

For transforming paths the proper way, there is an utility cygpath.

Below is source of the rpmbuild.bat. But it fails to compile on the line with string comparison as proposed 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

How to fix?

Upvotes: 0

Views: 1476

Answers (1)

Endoro
Endoro

Reputation: 37569

  1. here you SET "str1 "=" %%P", two unwanted spaces
  2. here IF NOT x%str1:_topdir=%==x%str1% you are inside a for code block where you need delayed expansion !variables!, this error occures more often in your script.

Upvotes: 2

Related Questions