如果ELSE问题命令批处理

时间:2010-09-20 19:25:26

标签: scripting batch-file command windows-server

我在命令批处理脚本中遇到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%

4 个答案:

答案 0 :(得分:16)

您看到的错误来自错误的格式化。不要把它全部放在一行。而是使用它:

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

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

答案 1 :(得分:11)

您可以将其简化为:

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

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

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

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

答案 2 :(得分:3)

问题与显示的代码无关!

问题出在

BarCodeView *barCodeView = [[BarCodeView alloc] initWithFrame:kBarCodeFrame];
[self.view addSubview:barCodeView];
[barCodeView setBarCode:GetNewRandomEAN13BarCode()];

由于未定义变量:COUNTER IF %i%==0 ( GOTO :RESETROUTER ) ELSE ( ,该行将扩展为

i

这是一个无效的表达。

要找到这样简单的语法失败,您应该使用IF ==0 ( GOTO :RESETROUTER ) ELSE ( 启用调试。

答案 3 :(得分:0)

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