gpt4 book ai didi

variables - 不带值的批处理脚本 SET/p

转载 作者:行者123 更新时间:2023-12-02 11:29:02 27 4
gpt4 key购买 nike

在 Windows XP 中创建批处理脚本。这是我遇到问题的代码片段:

::==============================================================::
::How many scripts are included in this program?
SET NumOfScripts=4
::==============================================================::

:mainmenu
CLS
ECHO [MAIN MENU]
ECHO Please choose from the following options to run a particular script:
set /p choice="[1] SCRIPT1.bat | [2] SCRIPT2.bat | [3] SCRIPT3.bat | [4] SCRIPT4.bat : "

IF %choice% EQU 1 CALL :SCRIPT1
IF %choice% EQU 2 CALL :SCRIPT2
IF %choice% EQU 3 CALL :SCRIPT3
IF %choice% EQU 4 CALL :SCRIPT4

REM Wrong Choices
IF %choice% GTR %NumOfScripts% (
(ECHO You have entered an invalid option. Please press any key to be taken back to the main menu.) & PAUSE & GOTO:mainmenu
)
IF %choice% LEQ 0 (
(ECHO You have entered an invalid option. Please press any key to be taken back to the main menu.) & PAUSE & GOTO:mainmenu
)
ECHO You have entered an invalid option. Please press any key to be taken back to the main menu
PAUSE & GOTO:mainmenu

查看REM Wrong Choice,前两个参数按其应有的方式工作,但是,如果用户未输入任何值(只需按 Enter 键),它会自动终止脚本。我添加了 IF NOT DEFINED choice 但这不起作用...我还尝试了 IF [%choice%]==[]IF [ %choice%] EQU [] 这些也不起作用。

有趣的是......你输入一个无效的数字,比如5或-1,它会给出回声并返回到主菜单,因为它应该......那么如果你只是按回车键而不插入值,它会回显并返回主菜单。

我的问题是如何让它识别出用户第一次没有输入 set/p 的值?

最佳答案

您可以在set/p之前将choice初始化为某个无效值,例如:

SET choice=none

要打印相应的错误消息,您可以这样做

IF %choice% EQU none (
(ECHO You did not select an option.) & PAUSE & GOTO:mainmenu
)

关于variables - 不带值的批处理脚本 SET/p,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7405981/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com