=y was unexpected at this time

时间:2016-07-11 23:04:37

标签: windows batch-file

I'm trying to make a script to autoinstall packages on Windows, and I keep getting

=y was unexpected at this time

What's wrong with it?

@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

Note: I use "sudo.exe" to elevate privileges. I'm not trying to use bash in windows.

2 个答案:

答案 0 :(得分:2)

I keep getting "=y was unexpected at this time" What's wrong with it?

if %internet%=y goto start

= is not a valid comparison operator, you should be using ==:

if %internet%==y goto start

That applies to all of your if %internet% commands.


Syntax

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.

Further Reading

答案 1 :(得分:0)

您可以这样做:

@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