Cmd跳过if语句? [英] Cmd skips if-statement?

查看:397
本文介绍了Cmd跳过if语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很无聊所以我试图在CMD中创建一个虚拟控制台,它似乎工作正常,直到检查输入并相应地执行命令。



我得到一个显示:的屏幕,你可以输入你要执行的命令,输入正常但是当它检查要执行的命令时,会出现问题。



问题是如果我键入register,它应该转到一个名为:reg的标签,我执行exit命令,该命令只应在我输入exit时执行。



这是我的剧本:



 echo off 
cls

:console
title命令提示符
cls
set / pc =:

如果%c%==退出

退出


如果%c%==注册


goto:reg




:reg
title注册一个新账户
cls
echo注册
echo。

set / p newName =Username:
set / p newPass =Password:
cls
echo Processing ...
> nul超时/ t 3
转到控制台





我尝试了什么:



我试过改变命令和if语句的顺序但看起来第一个命令总是被执行而不管你键入什么。

解决方案

尝试这个,暂停和回声验证:



 cls 

:console
title命令提示符
cls
set / pc =:
echo%c%
暂停

如果%c%==退出转到结束

如果%c%==注册转到reg

暂停

:reg
title注册一个新账户
cls
echo注册
echo。

set / p newName =Username:
set / p newPass =Password:
cls
echo Processing ...
> nul timeout / t 3
goto console
:end


从所有内容中删除引号,不需要使用brakets



 echo off 
cls

:console
title命令提示符
cls
set / pc =:

if%c%== exit exit

if%c%== register goto:reg


:reg
title注册一个新账户
cls
echo注册
echo。

set / p newName =Username:
set / p newPass =Password:
cls
echo Processing ...
> nul timeout / t 3
goto console


如果是多元的话,你在哪里找到线?和()

这是一个语法链接:

更强大的批处理文件 - 使用If语句进行分支 [ ^ ]



Windows命令行| Shell列表和参考 [ ^ ]

I was bored so I tried to make a virtual console inside CMD and it appears to be working fine up until the point of checking the input and executing a command accordingly.

I get a screen that says ": " and there you can type what command you want to execute, and typing works fine but when it goes to check what command to execute, problems occurr.

The problem is if I type "register" which should go to a label called :reg, I execute the "exit" command, which should only be executed when I type "exit".

Here's my script:

echo off
cls

:console
title Command prompt
cls
set /p c=": "

if "%c%"=="exit"
(
exit
)

if "%c%"=="register"
(

goto :reg

)


:reg
title Register a new account
cls
echo Registration
echo.

set /p newName="Username: "
set /p newPass="Password: "
cls
echo Processing...
>nul timeout /t 3
goto console



What I have tried:

I've tried changing the order of the commands and if-statements but it appears that the first command always gets executed no matter what you type.

解决方案

Try this, stuck pauses in and echo to verify:

cls
 
:console
title Command prompt
cls
set /p c=": "
echo %c%
pause

if "%c%"=="exit" goto end
 
if "%c%"=="register" goto reg

pause 

:reg
title Register a new account
cls
echo Registration
echo.
 
set /p newName="Username: "
set /p newPass="Password: "
cls
echo Processing...
>nul timeout /t 3
goto console
:end


Remove the quotes from everything and there's no need for brakets

echo off
cls
 
:console
title Command prompt
cls
set /p c=: 
 
if %c%==exit exit
 
if %c%==register goto :reg

 
:reg
title Register a new account
cls
echo Registration
echo.
 
set /p newName="Username: "
set /p newPass="Password: "
cls
echo Processing...
>nul timeout /t 3
goto console


Where did you catch that if is multi line ? and with () ?
Here is a link for the syntax:
More Powerful Batch Files- Branching with "If" statements[^]

Windows Command Line |Shell List and Reference[^]


这篇关于Cmd跳过if语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆