最近做WINDOWS批处理发现有几点东西比较有用,特别是做*NIX久了,发现一些原本有的东西不知怎么执行,呵呵,现在记录一下,希望对大家有帮助。
参数传递
more abc.bat
echo %1 -BAD>>iptest.log
执行abc.bat 192.168.0.1
则将192.168.0.1 放入iptest.log
将命令结果存为变量
FOR /F %%A IN ('ping -n 3 -w 3000 192.168.0.1 ^|find "Reply" /C') DO SET /a Var=%%A
echo %Var%
则将ping 192.168.0.1x结果
C:\>ping 192.168.0.1 -n 3
Pinging 192.168.0.1 with 32 bytes of data:
Reply from 192.168.0.1: bytes=32 time<1ms TTL=255
Reply from 192.168.0.1: bytes=32 time<1ms TTL=255
Reply from 192.168.0.1: bytes=32 time<1ms TTL=255
Ping statistics for 192.168.0.1:
Packets: Sent = 3, Received = 3, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 0ms, Maximum = 0ms, Average = 0ms
C:\>
中Reply 进行统计。本例中为3
命令返回值
%ERRORLEVEL% will expand into a string representation of
the current value of ERRORLEVEL, provided that there is not already
an environment variable with the name ERRORLEVEL, in which case you
will get its value instead. After running a program, the following
illustrates ERRORLEVEL use:
goto answer%ERRORLEVEL%
:answer0
echo Program had return code 0
:answer1
echo Program had return code 1
You can also using the numerical comparisons above:
IF %ERRORLEVEL% LEQ 1 goto okay
看来WINDOWS中命令成功与否也有return 数值嘛,试一下看看
C:\> ping 192.168.0.1
Pinging 192.168.0.1 with 32 bytes of data:
Reply from 192.168.0.1: bytes=32 time<1ms TTL=255
Reply from 192.168.0.1: bytes=32 time<1ms TTL=255
Reply from 192.168.0.1: bytes=32 time<1ms TTL=255
Reply from 192.168.0.1: bytes=32 time<1ms TTL=255
Ping statistics for 192.168.0.1:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 0ms, Maximum = 0ms, Average = 0ms
C:\>echo %ERRORLEVEL%
0
C:\> ping 192.168.200.1
Pinging 192.168.200.1 with 32 bytes of data:
Request timed out.
Request timed out.
Request timed out.
Request timed out.
Ping statistics for 192.168.200.1:
Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),
C:\>echo %ERRORLEVEL%
1
C:\>
阅读(743) | 评论(0) | 转发(0) |