简体   繁体   中英

Variable batch loop issue

I'm trying to get this batch script working but I'm having a bit of a problem with some commands. It keeps not taking the variable as valid and sending it to the invalid section instead. The weird thing is that I have the same variable processing section in another part of the code. Here's my code:

:comp
cls
ECHO.
ECHO =================================================
ECHO Please Select the Device you would like to ping:
ECHO =================================================
SET /P comp="> "
IF /I %comp%==1 goto reg
IF /I %comp%==2 goto reg2
IF /I %comp%==3 goto reg3
IF /I %comp%==4 goto reg4
IF /I %comp%==5 goto reg5
IF /I %comp%==6 goto reg6
IF /I %comp%==7 goto reg7
IF /I %comp%==8 goto reg8
IF /I %comp%==9 goto reg9
IF /I %comp%==0 goto reg0
IF NOT %comp%=='1''2''3''4''5''6''7''8''9''0' goto inval
goto _ping
:inval
cls
ECHO.
ECHO ======================================
ECHO Choice invalid, please try again.
ECHO ======================================
ECHO.
pause
goto comp
:reg
cls
ECHO.
ECHO =========================================================
ECHO Please select the specific you would like to connect to.
ECHO =========================================================
ECHO.
SET /P regi="> "
IF /I %regi%==1 set str3=141
IF /I %regi%==2 set str3=142
IF /I %regi%==3 set str3=143
IF /I %regi%==4 set str3=144
IF /I %regi%==5 set str3=145
IF /I %regi%==6 set str3=146
IF /I %regi%==7 set str3=147
IF /I %regi%==8 set str3=148
IF /I %regi%==9 set str3=149
IF NOT %regi%=='1''2''3''4''5''6''7''8''9''0' goto inval
goto _ping

So the first section works and sends to the right section. The second section is the invalid response section, and the third section is that one that processes the exact device (and last octet) to ping. IF you enter a valid choice it will still go to the if not and process to the inval section. Any Suggestions?

Issue #1

The problem is in this line:

IF NOT %regi%=='1''2''3''4''5''6''7''8''9''0' goto inval

Since regi is not equal to the string '1''2''3''4''5''6''7''8''9''0' (given a legal 0-9 input), the script will always jump to the inval section. Note that the logic behind this line is flawed: the user is unlikely to ever enter this as input, and I doubt that you wanted this behaviour.

Instead, I believe that you just want to check whether regi is a digit or not. The solution to this is simply initialize str3 to some initial value, and check it after the if statements:

str3=invalid
IF /I "%regi%"=="1" set str3=141
...
IF /I "%regi%"=="9" set str3=149
IF "%str3%"=="invalid" goto inval
goto _ping

If str3 retains the "invalid" value, this implies that the input is invalid because none of the if statements was executed.

Issue #2

The same goes for this line:

IF NOT %comp%=='1''2''3''4''5''6''7''8''9''0' goto inval

This checks comp to a very unlikely

If any of the if statements is executed, the script will jump to one of the "reg" labels. If the input is invalid, the if statements are skipped, so just put goto inval after them, like so:

IF /I "%comp%"=="1" goto reg
...
IF /I "%comp%"=="0" goto reg0
goto inval

This also makes the following goto ping line redundant.

Issue #3

I can also spot another issue in your script: if comp is an empty string, the following comparison (and the likes of it):

    IF /I %comp%==1 goto reg

will evaluate to this illegal expression:

IF /I ==1 goto reg

and will cause the interpreter to raise an error. It is recommended to concatenate fixed characters to both sides of the comparison to avoid this, for example like so:

IF /I "%comp%"=="1" goto reg1


Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM