【问题标题】:Batch file closes instead of doing GOTO command [closed]批处理文件关闭而不是执行 GOTO 命令 [关闭]
【发布时间】:2022-01-03 02:38:08
【问题描述】:

我的代码没有完全完成,但是在我做出选择后按 Enter 后测试它关闭了。这应该使用 goto 并转到 :WEBSITECHECK 但会关闭。

这是我的代码:

echo Welcome! This program will check if a website or server is up.
echo
echo Shortcuts:
echo 'fb' for facebook.com
echo 't' for twitter.com
echo 'insta' for instagram.com
echo 'yt' for youtube.com
echo 'r' for reddit.com 

echo -------------------------------------------------------------
echo Type '1' to check a website or '2' to check an I.P. address.
echo -------------------------------------------------------------

set type=
set /p %type% = Enter selection:
 if %type%==1 GOTO WEBSITECHECK
 if %type%==2 GOTO IPCHECK

:WEBSITECHECK

set site=
set /p %site% = Enter URL or shortcut:

REM shortcuts

if %site% == fb ping facebook.com
if %site% == t ping twitter.com
if %site% == insta ping instagram.com
if %site% == yt ping youtube.com
if %site% == r ping reddit.com

if %site% !==! fb GOTO OTHERSITE
if %site% !==! t GOTO OTHERSITE
if %site% !==! insta GOTO OTHERSITE
if %site% !==! yt GOTO OTHERSITE
if %site% !==! r GOTO OTHERSITE

:OTHERSITE

ping %site%

:IPCHECK

pause

【问题讨论】:

  • 你的set 命令应该是set /p type=Enter Selectionset site=Enter URL or shortcut。您删除了变量(如果它曾经存在)并且再也没有设置它,然后您尝试将不存在的变量与单词匹配,它将导致语法错误并退出。所以请打开cmd 并浏览到脚本的位置,然后从那里启动它而不是双击。你会看到发生了什么。无论如何,通常对于这样的任务,我会简单地使用choice。从cmd 看到choice /?。最后,我希望你为我解释一下。 if .. !==! ..?

标签: windows batch-file cmd terminal


【解决方案1】:

我已经在上面的评论中指出了你的问题,所以这里有一个例子说明choice 在这样的场景中是如何使用的。:

@echo off & set cnt=
setlocal enabledelayedexpansion
choice /C 12 /M "Site or IP?:
set "chose=%errorlevel%"

:: you can have a maximum of 9 sites!
set "select1=youtube.com facebook.com twitter.com instagram.com reddit.com"

:: You can have a maximum of 9 IP's!
set "select2=8.8.8.8 127.0.0.1 10.1.4.5"

for %%i in (!select%chose%!) do (
    set /a cnt+=1
    echo !cnt!^) %%i
    set "_opt!cnt!=%%i"
)
for /l %%c in (1,1,%cnt%) do set ch=!ch!%%c
choice /C %ch% /M "Enter Selection:"
ping !_opt%errorlevel%!
pause

【讨论】:

    猜你喜欢
    • 2017-11-10
    • 1970-01-01
    • 2013-09-24
    • 2016-02-08
    • 1970-01-01
    • 1970-01-01
    • 2020-09-29
    • 1970-01-01
    • 2012-01-21
    相关资源
    最近更新 更多