我试图做一个脚本来自动安装在Windows上的软件包,我不断收到
= y在这个时候是意外的
它出什么问题了?
@echo off echo Checking Internet... Ping www.google.com -n 1 -w 1000 cls if errorlevel 1 (set internet=n) else (set internet=y) if %internet%=y goto start if %internet%=n goto warn :warn echo Warning! You are not connected to the Internet. echo Chocolatey will not install until you connect and echo run this batchfile again. echo Press any key to continue anyways. pause >nul :start echo Copying... echo [sudo.exe] mkdir C:\pkg copy sudo.exe C:\pkg sudo xcopy C:\pkg\sudo.exe C:\Windows echo [chocolatey.bat] copy chocolatey.bat C:\pkg echo [package.bat] copy package.bat C:\pkg echo Installing Choco... if %internet%=y sudo C:\pkg\chocolatey.bat if %internet%=n echo Cancelled: No internet. echo Press any key when complete. pause >nul echo Installing Packages... if %internet%=y sudo C:\pkg\package.bat if %internet%=n echo Cancelled: No internet. echo Press any key when complete
注意:我使用“sudo.exe”来提升权限。 我不想在Windows中使用bash。
if %internet%=y goto start
=
不是有效的比较运算符,您应该使用==
:
if %internet%==y goto start
这适用于所有if %internet%
命令。
F:\test>if /? Performs conditional processing in batch programs. IF [NOT] ERRORLEVEL number command IF [NOT] string1==string2 command IF [NOT] EXIST filename command NOT Specifies that Windows should carry out the command only if the condition is false. ERRORLEVEL number Specifies a true condition if the last program run returned an exit code equal to or greater than the number specified. string1==string2 Specifies a true condition if the specified text strings match.
你可以做这样的事情:
@echo off echo Checking Internet... Ping www.stackoverflow.com -n 1 -w 1000 cls if "%errorlevel%" EQU "1" (set internet=N) else (set internet=Y) if /I "%internet%"=="y" goto start if /I "%internet%"=="n" goto warn :start echo start pause Exit /b :warn echo warn pause Exit /b