如何申请,并在蝙蝠接收用户输入并使用它来运行某个程序? [英] How do I request and receive user input in a .bat and use it to run a certain program?

查看:201
本文介绍了如何申请,并在蝙蝠接收用户输入并使用它来运行某个程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我迄今为止

@echo off
:Ask
echo Would you like to use developer mode?(Y/N)
set INPUT=
set /P INPUT=Type input: %=%
If %INPUT%=="y" goto yes 
If %INPUT%=="n" goto no
If %INPUT%=="Y" goto yes
If %INPUT%=="N" goto no
:yes
java -jar lib/RSBot-4030.jar -dev
echo Starting RSbot in developer mode
:no
java -jar lib/RSBot-4030.jar
echo Starting RSbot in regular mode
pause

无论采用哪种方式,如果用户输入y或n它总是在-dev模式运行。

Either way if the user enters y or n it always runs in -dev mode.

我如何让它在-dev模式下运行,如果答案是肯定的,常规模式如果答案是否定的。此外,我怎么让它再问如果输入不是Y,N,Y,或n?

How do I make it run in -dev mode if the answer is yes, and regular mode if the answer is no. Also, how do I make it ask again if the input isn't Y, N, y, or n?

推荐答案

如果输入的是,比方说, N ,您的IF行评价是这样的:

If the input is, say, N, your IF lines evaluate like this:

If N=="y" goto yes 
If N=="n" goto no
…

这是,你是比较 N Y,那么N 等,包括N。你永远不会得到匹配,除非用户以某种方式决定输入NY(即无论是四个大字,但双引号括起来)。

That is, you are comparing N with "y", then "n" etc. including "N". You are never going to get a match unless the user somehow decides to input "N" or "y" (i.e. either of the four characters, but enclosed in double quotes).

所以,你需要或者删除从各地 N N 或把他们周围%输入%在你的条件语句。我会推荐后者,因为这样你至少可以逃避一些在批处理脚本特殊意义(如果用户设法键入他们)的人物。所以,这是你应该得到什么:

So you need either to remove " from around y, n, Y and N or put them around %INPUT% in your conditional statements. I would recommend the latter, because that way you would be escaping at least some of the characters that have special meaning in batch scripts (if the user managed to type them in). So, this is what you should get:

If "%INPUT%"=="y" goto yes 
If "%INPUT%"=="n" goto no
If "%INPUT%"=="Y" goto yes
If "%INPUT%"=="N" goto no

顺便说一句,你可以通过应用减少一些条件/ I 切换到如果语句,像这样的:

By the way, you could reduce the number of conditions by applying the /I switch to the IF statement, like this:

If /I "%INPUT%"=="y" goto yes 
If /I "%INPUT%"=="n" goto no

/ I 开关,使比较不区分大小写的,所以你不需要为不同情况的字符串单独检查。

The /I switch makes the comparisons case-insensitive, and so you don't need separate checks for different-case strings.

另外一个问题是,执行发展方式命令后,有比其他命令中没有跳跃,因此,如果用户同意在开发模式下运行Java,他会得到它的运行无论是在发展模式和非发展模式。因此,也许你需要像这样添加到您的脚本:

One other issue is that, after the development mode command is executed, there's no jumping over the other command, and so, if the user agrees to run Java in the development mode, he'll get it run both in the development mode and the non-development mode. So maybe you need to add something like this to your script:

...
:yes
java -jar lib/RSBot-4030.jar -dev
echo Starting RSbot in developer mode
goto cont
:no
java -jar lib/RSBot-4030.jar
echo Starting RSbot in regular mode
:cont
pause

最后,以解决处理不正确的输入的问题,你可以简单地只是条件语句后添加另一个(无条件)转到命令,就在<$ C $前C>是标签,即转到提问,返回到显示的提示,并要求输入你的脚本的开始,或者你可能的的前跳再添echo命令,并解释说,输入是不正确的,是这样的:

Finally, to address the issue of processing incorrect input, you could simply add another (unconditional) goto command just after the conditional statements, just before the yes label, namely goto Ask, to return to the beginning of your script where the prompt is displayed and the input is requested, or you could also add another ECHO command before the jump, explaining that the input was incorrect, something like this:

@echo off
:Ask
echo Would you like to use developer mode?(Y/N)
set INPUT=
set /P INPUT=Type input: %=%
If /I "%INPUT%"=="y" goto yes 
If /I "%INPUT%"=="n" goto no
echo Incorrect input & goto Ask
:yes
...


注意:一些这里提到的也受到@xmjx在他们的答案解决的问题,这一点我完全承认

这篇关于如何申请,并在蝙蝠接收用户输入并使用它来运行某个程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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