IF ELSE 问题 COMMAND BATCH

发布于 2024-09-24 20:08:44 字数 2320 浏览 5 评论 0原文

我在命令批处理脚本中遇到有关 IF ELSE 的问题...

在记事本中:
代码:

:CHECKACCOUNT
if /I "%user%"=="insertusername" ( GOTO :ACCOUNT ) ELSE ( GOTO :CHECKPASSACCT )

:CHECKPASSACCT
if /I "%pass%"=="insertpassword" ( GOTO :ACCOUNT ) ELSE ( GOTO :COUNTER )

在命令中:
代码:

(此时出乎意料。

完整的脚本代码:

@echo off

::SETTINGS:::::::::::::::::::::::
set filetxt =userpass.txt
set log=logfile.log
set timer=900
::set default = true
::set user = 0
::set pass = 0
:::::::::::::::::::::::::::::::::


:STARTER
ECHO.>>%log%
ECHO ========START========>>%log%
SetLocal EnableDelayedExpansion
Set n=
Set _InputFile=%filetxt%
For /F "tokens=*" %%I IN (%_InputFile%) DO (
Set /a n+=1
Set acct!n!=%%I
)
set router_ip=%acct1%
set user=%acct2%
set pass=%acct3%
GOTO :CHECKFILE1


:CHECKFILE1
CLS
IF EXIST curl.exe ( GOTO :CHECKFILE2 ) else (
ECHO ERROR: curl.exe was not found.>>%log%
ECHO ERROR: curl.exe was not found.
ECHO.
ECHO.
GOTO :PAUSEEXIT
)

:CHECKFILE2
CLS
IF EXIST sleep.exe ( GOTO :CHECKACCOUNT ) else (
ECHO ERROR: sleep.exe was not found.>>%log%
ECHO ERROR: sleep.exe was not found.
ECHO.
ECHO.
GOTO :PAUSEEXIT
)

:CHECKACCOUNT
if /I "%user%"=="insertusername" GOTO ACCOUNT
GOTO CHECKPASSACCT

:CHECKPASSACCT
if /I "%pass%"=="insertpassword" GOTO ACCOUNT
GOTO COUNTER

:ACCOUNT
CLS
::if %default% = true ( GOTO :COUNTER ) ELSE (
ECHO To edit/change USERNAME and PASSWORD... Please type: OPTION
ECHO.
SET /P user="Please enter the username of your Router:"
IF /I %user%==OPTION ( Goto :EDITBAT )
CLS
ECHO To edit/change USERNAME and PASSWORD... Please type: OPTION
ECHO.
SET /P pass="Please enter the password of your Router:"
IF /I %pass%==OPTION ( Goto :EDITBAT )
CLS
set /a i = 1
GOTO :CHECKACCOUNT
::)

:EDITBAT
start /WAIT notepad %filetxt%
set router_ip=%acct1%
set user=%acct2%
set pass=%acct3%
GOTO :CHECKACCOUNT

:COUNTER
IF %i%==0 ( GOTO :RESETROUTER ) ELSE (
ECHO WAIT %i% seconds...
sleep 1
set /a i = i - 1
CLS
GOTO :COUNTER
)


:RESETROUTER
CLS
ECHO READY to RESET....
ECHO Preparing....
sleep 2

sleep 2
CLS
ECHO Processing....
sleep 5

sleep 2
CLS
ECHO Success....
sleep 5
set /a i = %timer%
CLS
GOTO :COUNTER

:PAUSEEXIT
PAUSE

:EXIT
ECHO.>>%log%
ECHO ========END OF LOG FILE========>>%log%

I have problem about IF ELSE in Command Batch script...

In Notepad:
Code:

:CHECKACCOUNT
if /I "%user%"=="insertusername" ( GOTO :ACCOUNT ) ELSE ( GOTO :CHECKPASSACCT )

:CHECKPASSACCT
if /I "%pass%"=="insertpassword" ( GOTO :ACCOUNT ) ELSE ( GOTO :COUNTER )

In COMMAND:
Code:

( was unexpected at this time.

FULL Script Code:

@echo off

::SETTINGS:::::::::::::::::::::::
set filetxt =userpass.txt
set log=logfile.log
set timer=900
::set default = true
::set user = 0
::set pass = 0
:::::::::::::::::::::::::::::::::


:STARTER
ECHO.>>%log%
ECHO ========START========>>%log%
SetLocal EnableDelayedExpansion
Set n=
Set _InputFile=%filetxt%
For /F "tokens=*" %%I IN (%_InputFile%) DO (
Set /a n+=1
Set acct!n!=%%I
)
set router_ip=%acct1%
set user=%acct2%
set pass=%acct3%
GOTO :CHECKFILE1


:CHECKFILE1
CLS
IF EXIST curl.exe ( GOTO :CHECKFILE2 ) else (
ECHO ERROR: curl.exe was not found.>>%log%
ECHO ERROR: curl.exe was not found.
ECHO.
ECHO.
GOTO :PAUSEEXIT
)

:CHECKFILE2
CLS
IF EXIST sleep.exe ( GOTO :CHECKACCOUNT ) else (
ECHO ERROR: sleep.exe was not found.>>%log%
ECHO ERROR: sleep.exe was not found.
ECHO.
ECHO.
GOTO :PAUSEEXIT
)

:CHECKACCOUNT
if /I "%user%"=="insertusername" GOTO ACCOUNT
GOTO CHECKPASSACCT

:CHECKPASSACCT
if /I "%pass%"=="insertpassword" GOTO ACCOUNT
GOTO COUNTER

:ACCOUNT
CLS
::if %default% = true ( GOTO :COUNTER ) ELSE (
ECHO To edit/change USERNAME and PASSWORD... Please type: OPTION
ECHO.
SET /P user="Please enter the username of your Router:"
IF /I %user%==OPTION ( Goto :EDITBAT )
CLS
ECHO To edit/change USERNAME and PASSWORD... Please type: OPTION
ECHO.
SET /P pass="Please enter the password of your Router:"
IF /I %pass%==OPTION ( Goto :EDITBAT )
CLS
set /a i = 1
GOTO :CHECKACCOUNT
::)

:EDITBAT
start /WAIT notepad %filetxt%
set router_ip=%acct1%
set user=%acct2%
set pass=%acct3%
GOTO :CHECKACCOUNT

:COUNTER
IF %i%==0 ( GOTO :RESETROUTER ) ELSE (
ECHO WAIT %i% seconds...
sleep 1
set /a i = i - 1
CLS
GOTO :COUNTER
)


:RESETROUTER
CLS
ECHO READY to RESET....
ECHO Preparing....
sleep 2

sleep 2
CLS
ECHO Processing....
sleep 5

sleep 2
CLS
ECHO Success....
sleep 5
set /a i = %timer%
CLS
GOTO :COUNTER

:PAUSEEXIT
PAUSE

:EXIT
ECHO.>>%log%
ECHO ========END OF LOG FILE========>>%log%

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

笙痞 2024-10-01 20:08:44

您看到的错误来自错误的格式。不要将所有内容都放在一行上。相反,使用这个:

if /I "%user%"=="insertusername" (
     GOTO :ACCOUNT 
) ELSE (
     GOTO :CHECKPASSACCT 
)

更深层的根本原因是:goto :somewhere 命令需要以换行符终止。

Your error as seen comes from wrong formatting. Don't put it all on 1 line. Instead use this:

if /I "%user%"=="insertusername" (
     GOTO :ACCOUNT 
) ELSE (
     GOTO :CHECKPASSACCT 
)

The deeper, underlying reason is: goto :somewhere command needs to be terminated by a newline.

池予 2024-10-01 20:08:44

您可以将其简化为:

:CHECKACCOUNT
if /I "%user%"=="insertusername" GOTO ACCOUNT
GOTO CHECKPASSACCT

:CHECKPASSACCT
if /I "%pass%"=="insertpassword" GOTO ACCOUNT
GOTO COUNTER

不需要 ELSE 语句。由于 IF 块会跳转到其他地方,因此将第二个 GOTO 放在下一行或 ELSE 块中应该是等效的。

此外,在定义 GOTO 目标时需要前导冒号,但在 GOTO 语句本身内引用目标名称时则不需要。

You can simplify this down to:

:CHECKACCOUNT
if /I "%user%"=="insertusername" GOTO ACCOUNT
GOTO CHECKPASSACCT

:CHECKPASSACCT
if /I "%pass%"=="insertpassword" GOTO ACCOUNT
GOTO COUNTER

The ELSE statements aren't needed. Since the IF block will jump somewhere else, placing the second GOTO on the next line or in an ELSE block should be equivalent.

Also, you need the leading colon when defining a GOTO target but not when referring to the target name inside the GOTO statement itself.

黑凤梨 2024-10-01 20:08:44

该问题与显示的代码无关!

问题在于,

:COUNTER
IF %i%==0 ( GOTO :RESETROUTER ) ELSE (

由于变量 i 未定义,该行将扩展为

IF ==0 ( GOTO :RESETROUTER ) ELSE (

That is a invalid expression。

要发现如此简单的语法失败,您应该使用 ECHO ON 启用调试。

The problem has nothing to do with the shown code!

The problem is at

:COUNTER
IF %i%==0 ( GOTO :RESETROUTER ) ELSE (

As the variable i isn't defined, the line will be expanded to

IF ==0 ( GOTO :RESETROUTER ) ELSE (

That is an invalid expression.

To find such simple syntax fails, you should enable debugging with ECHO ON.

夏の忆 2024-10-01 20:08:44

您不需要使用括号。你基本上是在告诉系统
“如果 %user% 是 insertusername (”,它询问“‘(’应该做什么?”

You don't need to use the parentheses. You're basically telling the system
"if %user% is insertusername (" and it's asking "what is the '(' supposed to do?"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文