0

I'm attempting to access the values for each index in an array. Here's the code I've written:

@echo off

set mode=development
set ssh_port=22

@REM List of scripts to run
set "scripts[0]=new-user"
set "scripts[1]=provision"
set "scripts[2]=firewall"
set "scripts[3]=cleanup"
set "scripts[4]=activate-firewall"

if "%mode%" == "development" (

    @REM Copy scripts to server
    scp -P %ssh_port% -F ssh_config -r scripts dev:/tmp

    @REM Enable SSH variables
    ssh -p %ssh_port% -F ssh_config dev "sudo bash /tmp/enable-ssh-variables.sh"

    for /L %%x in (0, 1, 4) do (
       ssh -p %ssh_port% -F ssh_config dev "sudo bash /tmp/%%scripts[%%x]%%.sh"
    )

) else (
   echo "Not implemented"
)

Inside the look I expect the command to look like this:

ssh -p 22 -F ssh_config dev "sudo bash /tmp/new-user.sh"

But, instead I get an error:

bash: /tmp/%scripts[0]%.sh: No such file or directory

I get the same error when attempting to call the script over SSH. It's not expanding.

How can I expand the variable within the string in my batch script?

BugHunterUK
  • 7,707
  • 12
  • 55
  • 103
  • 2
    Precede the problematic line with `call`, then it should work; an even better way is [delayed variable expansion](https://ss64.com/nt/delayedexpansion.html): put `setlocal EnableDelayedExpansion` before said line, `endlocal` after it, and then replace `%%scripts[%%x]%%.sh` with `!scripts[%%x]!.sh`… – aschipfl Aug 09 '21 at 19:32

0 Answers0