Batch file :: if lss is not working properly

≯℡__Kan透↙ 提交于 2019-12-13 03:56:41

问题


My code is pretty much self explanatory :

@echo off
set /a n=0
:doitagain
set /a n+=1
echo Pinging : 10.0.0.%n%
ping 10.0.0.%n%
if %n% lss 255 goto doitagain

pause >nul

but it always pinging 10.0.0.1 so I guess tiny tweak needed here...


回答1:


As npocmaka stated in his comment you always need delayed expansion when you are working with variables which are modified inside a loop. The first step is to add SETLOCAL EnableDelayedExpansion somwhere at teh beginning of your code. The second step is to access the variables which are modified inside the loop with !varname! instead of %varname%. In your case it would be something like this:

@ECHO OFF
SETLOCAL EnableDelayedExpansion
...
set /a n=!n!+1
echo Pinging : 10.0.0.!n!
ping 10.0.0.!n!
if !n! lss 255 goto doitagain
...


来源:https://stackoverflow.com/questions/33366300/batch-file-if-lss-is-not-working-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!