批处理:命令的语法不正确。

时间:2021-07-07 21:12:51

I recently started trying to make a "login" in bacth by using some variables and if statements. What happened is when I run this code in CMD and I try to log in without having made a username or password, it should bring me to the label "FAIL" but instead it gives me the error "The syntax of the command is incorrect." While this doesn't tell me what lines I messed up on, I think its finding a problem with the IF/ELSE statements in the "LOGIN" label. Have a look for me, see if you can figure out what I'm doing wrong.

我最近开始尝试用一些变量和if语句来“登录”bacth。发生的情况是当我在CMD中运行这段代码时,我试着登录而不需要用户名或密码,它应该会把我带到标签“FAIL”中,但是它给了我错误“命令的语法是不正确的”。虽然这并没有告诉我我搞砸了哪些行,但我认为它在“登录”标签中的IF/ELSE语句中发现了一个问题。帮我找一下,看看你能不能弄明白我做错了什么。

@ECHO off
SET /P CHOICE=Do you have a username/password? Y/N
IF %CHOICE%==y (
    GOTO LOGIN
)
IF %CHOICE%==Y (
    GOTO LOGIN
)
IF %CHOICE%==n (
    GOTO SIGNUP
)
IF %CHOICE%==N ( 
    GOTO SIGNUP
)
:SIGNUP
CLS
ECHO Please choose your username/password below
SET /P USER1=Username:
SET /P PASS1=Password:
CLS
ECHO Your log-in credentials should be set now
SET /P CHOICE=Do you want to log-in? Y/N
IF %CHOICE%==y (
    GOTO LOGIN
)
IF %CHOICE%==Y (
    GOTO LOGIN
)
IF %CHOICE%==n (
    GOTO END
)
IF %CHOICE%==N ( 
    GOTO END
)
:LOGIN
CLS
ECHO Please type in your username/password below
SET /P USER2=Username:
SET /P PASS2=Password:
IF EXIST %USER1% (
    IF %USER2%==%USER1% (
        GOTO SUCCESS
    ) ELSE (
        GOTO FAIL
    )
) ELSE (
    GOTO FAIL
)
IF EXIST %PASS1% (
    IF %PASS2%==%PASS1% (
        GOTO SUCCESS
    ) ELSE (
        GOTO FAIL
    ) 
) ELSE (
    GOTO FAIL
)
:SUCCESS
CLS
ECHO You have been successfully logged in
ECHO.
ECHO Press any key to continue
PAUSE>NUL
GOTO END
:FAIL
CLS
ECHO The username/password you entered was incorrect or does not exist
ECHO.
SET /P RETRY=Try again? Y/N
IF %RETRY%==y (
    GOTO LOGIN
)
IF %RETRY%==Y (
    GOTO LOGIN
)
IF %RETRY%==n (
    GOTO END
)
IF %RETRY%==N (
    GOTO END
)
:END
CLS
ECHO Press any key to exit
PAUSE>NUL

1 个解决方案

#1


3  

Here's the trick I use, though it's kind of odd:

这是我使用的技巧,虽然有点奇怪:

IF "%USER1%"=="%USER2%" (REM Do stuff...)

For input bob and bob, it resolves to this:

对于输入bob和bob,它解析为:

IF "bob"=="bob" (REM Do stuff...)

For input bob and joe, it resolves to this:

对于输入bob和joe,它解析为:

IF "bob"=="joe" (REM Do stuff...)

For input and joe, it resolves to this:

输入‌和乔,它解决了:

IF ""=="joe" (REM Do stuff...)

You could also test for empty input like this:

您还可以测试这样的空输入:

IF "%USER1%"=="" (Echo Please enter something.)

The IF statement was complaining when given nothing because it was getting IF ==joe, which is a syntax error.

IF语句在没有给出任何东西时是在抱怨,因为它得到了IF ==joe,这是一个语法错误。


Aditionally, I noticed you were doing IF EXIST %Pass1%, which isn't correct. Per the documentation in if /?:

从理论上讲,我注意到你在做的是如果存在%Pass1%,这是不正确的。如果/?

EXIST filename Specifies a true condition if the specified filename exists.

如果指定的文件名存在,则filename指定一个真实的条件。

You (appear to be) using this to check if the variable is defined. You do this (as I said before) with IF "%VAR1%"=="%VAR2%".

您(似乎是)使用这个来检查变量是否被定义。你这样做(我之前说过),如果“%VAR1%”==“%VAR2%”。


As a minor note (though I didn't correct it below), you can do if /I "%str1%"=="y" to do case-insensitive checking (IE it matches both y and Y) so that you don't need redundant code.

作为一个次要的注意事项(尽管我没有更正它),您可以做if /I“%str1%”==“y”来做不区分大小写的检查(它匹配y和y),这样您就不需要冗余代码。


Here's your entire script formatted to behave like this:

这是你的整个脚本格式化为这样:

@ECHO off
SET /P CHOICE=Do you have a username/password? Y/N
IF "%CHOICE%"=="y" (
    GOTO LOGIN
)
IF "%CHOICE%"=="Y" (
    GOTO LOGIN
)
IF "%CHOICE%"=="n" (
    GOTO SIGNUP
)
IF "%CHOICE%"=="N" ( 
    GOTO SIGNUP
)
:SIGNUP
CLS
ECHO Please choose your username/password below
SET /P USER1=Username:
SET /P PASS1=Password:
CLS
ECHO Your log-in credentials should be set now
SET /P CHOICE=Do you want to log-in? Y/N
IF "%CHOICE%"=="y" (
    GOTO LOGIN
)
IF "%CHOICE%"=="Y" (
    GOTO LOGIN
)
IF "%CHOICE%"=="n" (
    GOTO END
)
IF "%CHOICE%"=="N" ( 
    GOTO END
)
:LOGIN
CLS
ECHO Please type in your username/password below
SET /P USER2=Username:
SET /P PASS2=Password:
IF "%USER2%"=="%USER1%" (
    GOTO SUCCESS
) ELSE (
    GOTO FAIL
)
IF "%PASS2%"=="%PASS1%" (
    GOTO SUCCESS
) ELSE (
    GOTO FAIL
)
:SUCCESS
CLS
ECHO You have been successfully logged in
ECHO.
ECHO Press any key to continue
PAUSE>NUL
GOTO END
:FAIL
CLS
ECHO The username/password you entered was incorrect or does not exist
ECHO.
SET /P RETRY=Try again? Y/N
IF "%RETRY%"=="y" (
    GOTO LOGIN
)
IF "%RETRY%"=="Y" (
    GOTO LOGIN
)
IF "%RETRY%==n" (
    GOTO END
)
IF "%RETRY%"=="N" (
    GOTO END
)
:END
CLS
ECHO Press any key to exit
PAUSE>NUL

#1


3  

Here's the trick I use, though it's kind of odd:

这是我使用的技巧,虽然有点奇怪:

IF "%USER1%"=="%USER2%" (REM Do stuff...)

For input bob and bob, it resolves to this:

对于输入bob和bob,它解析为:

IF "bob"=="bob" (REM Do stuff...)

For input bob and joe, it resolves to this:

对于输入bob和joe,它解析为:

IF "bob"=="joe" (REM Do stuff...)

For input and joe, it resolves to this:

输入‌和乔,它解决了:

IF ""=="joe" (REM Do stuff...)

You could also test for empty input like this:

您还可以测试这样的空输入:

IF "%USER1%"=="" (Echo Please enter something.)

The IF statement was complaining when given nothing because it was getting IF ==joe, which is a syntax error.

IF语句在没有给出任何东西时是在抱怨,因为它得到了IF ==joe,这是一个语法错误。


Aditionally, I noticed you were doing IF EXIST %Pass1%, which isn't correct. Per the documentation in if /?:

从理论上讲,我注意到你在做的是如果存在%Pass1%,这是不正确的。如果/?

EXIST filename Specifies a true condition if the specified filename exists.

如果指定的文件名存在,则filename指定一个真实的条件。

You (appear to be) using this to check if the variable is defined. You do this (as I said before) with IF "%VAR1%"=="%VAR2%".

您(似乎是)使用这个来检查变量是否被定义。你这样做(我之前说过),如果“%VAR1%”==“%VAR2%”。


As a minor note (though I didn't correct it below), you can do if /I "%str1%"=="y" to do case-insensitive checking (IE it matches both y and Y) so that you don't need redundant code.

作为一个次要的注意事项(尽管我没有更正它),您可以做if /I“%str1%”==“y”来做不区分大小写的检查(它匹配y和y),这样您就不需要冗余代码。


Here's your entire script formatted to behave like this:

这是你的整个脚本格式化为这样:

@ECHO off
SET /P CHOICE=Do you have a username/password? Y/N
IF "%CHOICE%"=="y" (
    GOTO LOGIN
)
IF "%CHOICE%"=="Y" (
    GOTO LOGIN
)
IF "%CHOICE%"=="n" (
    GOTO SIGNUP
)
IF "%CHOICE%"=="N" ( 
    GOTO SIGNUP
)
:SIGNUP
CLS
ECHO Please choose your username/password below
SET /P USER1=Username:
SET /P PASS1=Password:
CLS
ECHO Your log-in credentials should be set now
SET /P CHOICE=Do you want to log-in? Y/N
IF "%CHOICE%"=="y" (
    GOTO LOGIN
)
IF "%CHOICE%"=="Y" (
    GOTO LOGIN
)
IF "%CHOICE%"=="n" (
    GOTO END
)
IF "%CHOICE%"=="N" ( 
    GOTO END
)
:LOGIN
CLS
ECHO Please type in your username/password below
SET /P USER2=Username:
SET /P PASS2=Password:
IF "%USER2%"=="%USER1%" (
    GOTO SUCCESS
) ELSE (
    GOTO FAIL
)
IF "%PASS2%"=="%PASS1%" (
    GOTO SUCCESS
) ELSE (
    GOTO FAIL
)
:SUCCESS
CLS
ECHO You have been successfully logged in
ECHO.
ECHO Press any key to continue
PAUSE>NUL
GOTO END
:FAIL
CLS
ECHO The username/password you entered was incorrect or does not exist
ECHO.
SET /P RETRY=Try again? Y/N
IF "%RETRY%"=="y" (
    GOTO LOGIN
)
IF "%RETRY%"=="Y" (
    GOTO LOGIN
)
IF "%RETRY%==n" (
    GOTO END
)
IF "%RETRY%"=="N" (
    GOTO END
)
:END
CLS
ECHO Press any key to exit
PAUSE>NUL