.bat curl with用于抱怨(此时是意想不到的)



我有以下代码:

@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
    )
)

在IF语句内的第二个for循环行(第二个IF语句),它报错(此时未预料到)

解决这个问题的方法是"润滑脂";通过运行这个命令(直接在提示符上)

>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

只有这样它才能工作

>prysm1.bat program-real version
A subdirectory or file C:UsersHPDocumentsInvestmentsethboxprysmdist already exists.
Latest release is v1.4.2.
detected variable USE_MY_VERSION=fake
Using version fake.
Downloading beacon chain fake to C:UsersHPDocumentsInvestmentsethboxprysmdistprogram-fake-windows-amd64.exe as specified in USE_MY_VERSION

我知道这与变量在执行和解析时展开的方式有关,可能是括号,_或其他特殊字符引起的。但我尝试了几件事,问题仍然存在。(全部不工作)

  • 将整个for加上双引号">
  • 用括号括住
  • 没有在my_version中使用_。

任何想法?由于

更新下面的注释

脚本现在在@aschipfl的建议之后没有报错。然而,最后一个if stat的值不是true。使for返回结果以确保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

根据所有建设性的反馈,以下是最终的工作版本,总结如下:

  • 将所有if语句中的[]替换为"(引号),以保护空格和其他特殊字符。
  • 使用! !vs %%的http变量,因为它是在一个块与延迟扩展
  • 检查是否存在下载(404错误),然后继续下载。
@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!)
.... same code as above... 
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 -o nul -w "%%{http_code}" -L "https://prysmaticlabs.com/releases/beacon-chain-%my_version%-%system%-%arch%" ') do set "http=%%i" && echo %%i
        if "!http!"=="404" (
            echo ESC[35mNo program real found for %my_version%ESC[0m
            exit /b 1
        )   
        curl -L https://prysmaticlabs.com/releases/beacon-chain-%my_version%-%system%-%arch% -o %PROGRAM_REAL%
        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
    )
)

最新更新