Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows CMD - Code Statement As Value of Array Variable Not Working For IF Command

I want to create an automated testing array in Windows Command using a single code statement as the VALUE for each testing array record. Here is what the array definition looks like for the first two records in the $code_test[xx] array:

set $code_test[00]=if 5 lss 10 (echo IT WORKED)
set $code_test[01]=if 5 lss 10 (echo BUG OFF)

I want to execute the VALUE of each $code_test[xx] array record (which is a test code statement) using a for /l loop like this:

for /l %%g in (0,1,1) do (
    echo $code_test[0%%g]
    !$code_test[0%%g]!
    echo.
)

The complete code is:

@echo off
setlocal enabledelayedexpansion

set $code_test[00]=if 5 lss 10 (echo IT WORKED)
set $code_test[01]=if 5 lss 10 (echo BUG OFF)

for /l %%g in (0,1,1) do (
    echo $code_test[0%%g]
    !$code_test[0%%g]!
    echo.
)

echo.
pause

When I execute the complete code I get the following error message when the !$code_test[0%%g]! line of code is executed:

'if' is not recognized as an internal or external command, operable program or batch file

I've read the excellent article here about how the IF command is parsed, but I don't see anything that jumps out at me as to why my code is failing. Is it even possible to do what I'm trying to accomplish?

Any Help Is Appreciated!

UPDATE: I discovered that @sst is correct when I used the set command in the set $code_test[01]=if 5 lss 10 (set /a $var1+=1) line of code:

@echo off

setlocal enabledelayedexpansion

set $var1=0
set $code_test[00]=if 5 lss 10 (echo IT WORKED)
set $code_test[01]=if 5 lss 10 (set /a $var1+=1)

for /l %%g in (0,1,1) do (
    echo $code_test[0%%g]
    cmd /c !$code_test[0%%g]!
    echo $var1 = !$var1!
    echo.
)

echo.
pause

The set /a $var1+=1 command didn't increment the value of $var1 as expected. In fact, it places a "1" in front of the $var1 label when the line of code echo $var1 = !$var1! executes. I haven't tried the solution offered by @Magoo yet. I'd really like to use the cmd /c solution instead of calling a subroutine. Is there anything I can do to make the set command work with the cmd /c option?

like image 346
Bill Vallance Avatar asked Apr 25 '26 02:04

Bill Vallance


2 Answers

It can not work the way you tried, because the IF command is detected and handeled in phase 2.
But with delayed expansion it is too late.
This problem occurs only for the commands IF, FOR and REM.

´CALL %%$code_test%%` works in many cases, but fails also for the three commands and for code blocks.
It's unclear why it fails in that case, as the commands are parsed in phase 2 but they are not recognized.

The solution of @stephan uses a new cmd instance, it has the drawback, to lose all variable modifications every time the cmd instance finishes.

The solution of @Magoo uses therefore percent expansion, that should work in nearly all cases without problems.

like image 193
jeb Avatar answered Apr 26 '26 18:04

jeb


@echo off
setlocal enabledelayedexpansion

set $code_test[00]=if 5 lss 10 (echo IT WORKED)
set $code_test[01]=if 5 lss 10 (echo BUG OFF)

for /l %%g in (0,1,1) do (
    SET "$code_test=!$code_test[0%%g]!
    CALL :test_code
    echo.
)

echo.
GOTO :EOF

:test_code
%$code_test%
GOTO :eof

Another approach.

like image 26
Magoo Avatar answered Apr 26 '26 18:04

Magoo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!