I have the following code.
@echo off
SetLocal EnableDelayedExpansion & REM All variables are set local to this run & expanded at execution time rather than at parse time (tip: echo !output!)
REM Get full path to run.bat file (excluding filename)
set wrapper_dir=%~dp1dist
set arch=amd64.exe
set system=windows
mkdir %wrapper_dir%
REM get_my_version - Find the latest version available for download.
(for /f %%i in ('curl -f -s https://prysmaticlabs.com/releases/latest') do set my_version=%%i) || (echo [31mERROR: require an internet connection. ESC[0m && exit /b 1)
echo ESC[37mLatest release is %my_version%.ESC[0m
IF defined USE_MY_VERSION (
echo [33mdetected variable USE_MY_VERSION=%USE_MY_VERSION%[0m
set reason=as specified in USE_MY_VERSION
set my_version=%USE_MY_VERSION%
) else (
set reason=automatically selected latest available release
)
echo Using version %my_version%.
set PROGRAM_REAL=%wrapper_dir%\program-%my_version%-%system%-%arch%
if [%1]==[program-real] (
if exist %PROGRAM_REAL% (
echo ESC[32mBeacon chain is up to date.[0m
) else (
echo ESC[35mDownloading beacon chain %my_version% to %PROGRAM_REAL% %reason%[0m
for /f "delims=" %%i in ('curl --silent -w "%%{http_code}" -L "https://prysmaticlabs.com/releases/beacon-chain-%my_version%-%system%-%arch%" -o "%PROGRAM_REAL%" ') do set http=%%i
if %http%=="400" (
echo ESC[35mNo program real found for %my_version%ESC[0m
exit \b 1
)
curl --silent -L https://prysmaticlabs.com/releases/beacon-chain-%my_version%-%system%-%arch%.sha256 -o %wrapper_dir%\beacon-chain-%my_version%-%system%-%arch%.sha256
curl --silent -L https://prysmaticlabs.com/releases/beacon-chain-%my_version%-%system%-%arch%.sig -o %wrapper_dir%\beacon-chain-%my_version%-%system%-%arch%.sig
)
)
At the line of the second for loop inside the IF statement(second IF statement) it complains ( was unexpected at this time.
The way to work around it , is to "grease" the batch file by running this command (on the prompt directly)
>for /f "delims=" %i in ('curl --silent -w "%{http_code}" -L https://prysmaticlabs.com/releases/beacon-chain-%prysm_version%-%system%-%arch% -o gg.txt') do set http=%i
ONLY then does it work
>prysm1.bat program-real version
A subdirectory or file C:\Users\HP\Documents\Investments\ethbox\prysm\dist already exists.
Latest release is v1.4.2.
detected variable USE_MY_VERSION=fake
Using version fake.
Downloading beacon chain fake to C:\Users\HP\Documents\Investments\ethbox\prysm\dist\program-fake-windows-amd64.exe as specified in USE_MY_VERSION
I know it has to do with the way the variables are expanded at execution vs parse time and that maybe a bracket, _ or other special characters are causing this. But i tried a couple of things and the problem persists. (all did not work)
- quote the entire for with double quotes "
- use brakets around the for
- not using the _ in my_version.
Any ideas? thanks
UPDATES after comments below
The script now does not complain after @aschipfl 's suggestions. However the last if stat is not evaluating to true. Made the for echo the result to ensure value in if stat.
for /f "delims=" %%i in ('curl --silent -o nul -w "%%{http_code}" https://prysmaticlabs.com/releases/beacon-chain-%version%-%system%-%arch% ') do set "http=%%i" && echo %%i