0

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.

Antoine
  • 55
  • 9
  • 1
    The expression `%podName!iteration!%` should read `!podName%iteration%!`, because the index `%iteration%` must become expanded first… – aschipfl Aug 21 '22 at 19:07
  • @aschipfl It does work... As this is a comment I can't put your answer in solution. This is the first time I've done batch, I didn't mind trying this combination. Thanks a lot! – Antoine Aug 21 '22 at 19:14
  • 1
    Perhaps you should take a look at this: [Arrays, linked lists and other data structures in cmd.exe (batch) script](https://stackoverflow.com/a/10167990)… – aschipfl Aug 21 '22 at 20:42

1 Answers1

1
@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]

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Thank you very much for this detailed answer. It's the first time I'm doing batch and I must say that it makes me feel very strange compared to bash. As I don't really understand how to do what I want with this language, I look for the info quite badly, I confess. For the record, the comments in the last loop were just there to say that this is where I'm going to use !podName%%p! in my command – Antoine Aug 22 '22 at 18:57