我的批处理脚本:-
@echo off
:alpha
rasdial "connection_name" connection_id connection_pass> tmpFile
set /p myvar= < tmpFile
del tmpFile
echo %myvar%
if "%myvar%" == "You are already connected to Broadband Connection." (exit)
goto alpha预期此脚本将运行命令radial ...,然后将该命令的输出存储在临时文件tempFile中。然后将该文件中命令radial的存储输出分配给一个变量,并删除该文件。变量依次用于检查命令是否成功执行(如果命令成功执行,则You are already connected to Broadband Connection.将是输出)。
但是在执行这个批处理文件时,我得到了输出:-
Connecting to Broadband Connection...
The syntax of the command is incorrect.然后剧本就破了。而不是再次循环,直到命令get成功执行为止。
回显后的输出:-
rasdial "Broadband Connection" uname pass 1>tmpFile
set /p myvar= 0<tmpFile
del tmpFile
echo You are already connected to Broadband Connection.
You are already connected to Broadband Connection.
The syntax of the command is incorrect.
if "You are already connected to Broadband Connection.
Command completed successfully." == "You are already connected to Broadband Connection." (exit)发布于 2019-06-29 08:47:05
变量myvar似乎包含一个行提要(是的,这确实出乎意料),破坏了您的if语法。
您应该切换到另一种方法(由@Marged建议)
:alpha
rasdial "Broadband Connection" uname pass | find "You are already connected to Broadband Connection." && exit /b
timeout 1 >nul
goto :alpha这将直接将rasdial的输出连接到find命令。&&的工作方式是“如果find成功”(找到字符串),然后执行exit命令。
提示:永远不要在没有空闲时间的情况下构建一个无限循环(在这里使用timeout完成),以防止CPU 100%地运行。
https://stackoverflow.com/questions/56815689
复制相似问题