Home > Blockchain >  Increment a variable already incremented in another loop
Increment a variable already incremented in another loop

Time:08-24

I would like to automate the execution of several commands to retrieve files from pods, with oc client.

I succeeded in incrementing the name of my variables, but I can't increment them in another loop to get their content.

@echo off
set /p "nbPod=How many times should RSYNC be used?? "

set num=0

for /l %%p in (1, 1, %nbPod%) do (
    set /a "num = num   1"
    setlocal EnableDelayedExpansion
    set /p "podName!num!= Enter pod name for pod number %%p ? "
)

set /a iteration=1

:loop
if %iteration% leq %num% (
    setlocal EnableDelayedExpansion
    echo %podName!iteration!%
    ::oc rsync %podName!iteration!%:/my_command_0
    ::oc rsync %podName!iteration!%:/my_command_1 [etc]
    set /a "iteration = iteration   1"
    goto :loop
)

NB : I don't know why but set /a num =1 wasn't working.

CodePudding user response:

@ECHO OFF
SETLOCAL EnableDelayedExpansion
:: remove variables starting podname
FOR  /F "delims==" %%b In ('set podname 2^>Nul') DO SET "%%b="
set /p "nbPod=How many times should RSYNC be used?? "

for /l %%p in (1, 1, %nbPod%) do set /p "podName%%p= Enter pod name for pod number %%p ? "

for /l %%p in (1, 1, %nbPod%) do (
   echo !podName%%p!
   rem within a block, use REM, not :: as :: is a broken label which terminates the block
   rem oc rsync !podName%%p!:/my_command_0
   rem oc rsync !podName%%p!:/my_command_1 [etc]
)
GOTO :EOF

Always verify against a test directory before applying to real data.

A few little issues with your code.

setlocal makes a copy of the current environment and processing continues using that new environment. An endlocal statement disposes of the new environment and restores the original. Reaching physical end-of-file is an implicit endlocal.

Consequently, it's normal to follow the initial @echo off with a setlocal as any statements executed before the setlocal are retained in the environment after the batch ends, which can cause confusion for subsequent batches as the variables use may be unexpectedly set.

There is a limit to the number of nested open setlocals that can be used (about 300). Unlikely to be a problem in this case, but something to note.

nbpod is your limit, so there's no requirement to use num or iteration.

I've no idea what the comments in the final loop are, but they MUST be rem statements within a block (parenthesised sequence of commands). OK- probably they are commented-out what I'm actually doing lines....

The for /f ... %%b command will process the list generated by set command, which would be something like

podname1=one
podname2=two

The delims= option causes for/f to assign the (default) first "token" in the line (podname1,podname2) to %%b and "sets" the value to nothing, deleting any stray variables from the environment. The 2^>nul suppresses error messages if there are no current variables named podname....

Then input the names as podname1... according to %%p.

And read them back using delayedexpansion, the current value of [podname strung with %%p]

  • Related