batch-file - 如何显示用户在此命令中键入的内容?
问题描述
请帮忙!我在整个互联网上寻找答案。
这是我的代码:
@echo off
title var test
:question
set a1=This
set a2=Is
set a3=a
set a4=Var
set a5=Test
choice /c 12345 /m "press a number"
if errorlevel=5 set num=5&goto answer
if errorlevel=4 set num=4&goto answer
if errorlevel=3 set num=3&goto answer
if errorlevel=2 set num=2&goto answer
if errorlevel=1 set num=1&goto answer
:answer
echo now change the answer.
set /p a%num%=
FOR /F "tokens=1-6" %%1 IN ("%a1% %a2% %a3% %a4% %a5% a%num%") DO echo %%1 %%2 %%4 %%5.&echo You typed=%%6
pause
goto question
如您所见,我让用户选择 1 到 5 之间的数字来更改特定单词。但是当我尝试使用相同类型的代码来显示他输入的内容时不起作用:(
解决方案
环境变量不应该以数字开头,也应该避免使用数字作为循环变量。在命令提示符窗口中运行,输出是此命令的帮助,解释了如何使用, , , ...call /?
引用批处理文件参数,这解释了为什么以数字作为第一个字符的环境变量和以数字为首的循环变量通常不好即使在
FOR集合中也不会引用环境变量or or or or的值。它只是环境变量的名称。循环根本没有必要。%0
%1
%2
a%num%
a1
a2
a3
a4
a5
for
@echo off
title var test
:question
set "a1=This"
set "a2=Is"
set "a3=a"
set "a4=Var"
set "a5=Test"
%SystemRoot%\System32\choice.exe /C 12345E /N /M "Press a number in range 1-5 or E for exit: "
if errorlevel 6 goto :EOF
set "num=%ERRORLEVEL%"
set /P "a%num%=Now change the answer: "
echo %a1% %a2% %a3% %a4% %a5%.
call echo You typed: %%a%num%%%
pause
goto question
命令行call echo You typed: %%a%num%%%
由 Windows 命令处理器解析,然后在3
输入的数字上执行命令行call echo You typed: %a3%
。由于命令call
导致替换%a3%
为环境变量的值,因此第二次解析此命令行a3
,因此echo
输出预期的字符串。
也可以替换call echo You typed: %%a%num%%%
为
setlocal EnableDelayedExpansion
echo You typed: !a%num%!
endlocal
使用延迟的环境变量扩展也会导致在执行 command 之前对命令行进行双重解析echo
。有关更多详细信息,请参阅Windows 命令解释器 (CMD.EXE) 如何解析脚本?
另请阅读此答案以获取有关命令SETLOCAL和ENDLOCAL的详细信息。
考虑到用户真的可以输入任何东西,上面批处理代码中的下面两行也不是很好。
echo %a1% %a2% %a3% %a4% %a5%.
call echo You typed: %%a%num%%%
例如,如果用户输入数字1
并在下一个提示符下输入:
Your user name is:& setlocal EnableDelayedExpansion & echo !UserName!& endlocal & rem
然后批处理文件执行与设计完全不同的操作并输出用户的帐户名称。
安全将是批处理代码:
@echo off
title var test
setlocal EnableExtensions DisableDelayedExpansion
:question
set "a1=This"
set "a2=Is"
set "a3=a"
set "a4=Var"
set "a5=Test"
%SystemRoot%\System32\choice.exe /C 12345E /N /M "Press a number in range 1-5 or E for exit: "
if errorlevel 6 goto :EOF
set "num=%ERRORLEVEL%"
set /P "a%num%=Now change the answer: "
setlocal EnableDelayedExpansion
echo !a1! !a2! !a3! !a4! !a5!.
echo You typed: !a%num%!
endlocal
pause
goto question
现在用户输入字符串不能再修改由 Windows 命令处理器执行的命令行。
使用无用FOR循环的解决方案是:
setlocal EnableDelayedExpansion
for /F tokens^=1-6^ eol^= %%A in ("!a1! !a2! !a3! !a4! !a5! !a%num%!") do echo %%A %%B %%C %%D %%E.&echo You typed: %%F
endlocal
eol=
1
如果使用输入数字和下一个以分号开头的字符串,则必须输出正确的所有内容。在这种情况下, FOR选项字符串不能用双引号引起来,"tokens=1-6 eol="
因为这将定义"
为行尾字符,如果用户输入数字1
并输入下一个以 开头的字符串,则不会输出任何内容"
。在执行 command 之前,必须通过双重解析整个命令行来将等号和空格转义^
为文字字符。cmd.exe
for
for
注意:FOR循环解决方案在用户为第一个变量值输入上面发布的特殊命令行字符串时无法正常工作。所以它也不是很安全。
推荐阅读
- amazon-web-services - 将 ElasticSearch 添加为 AWS Data Migration Service 的目标时连接失败
- javascript - Mongoose中的日期范围查询将循环结构转换为JSON
- r - 如何更改ggplot2中的轴线大小?
- java - Java中的中位数和平方根
- python - 如何使用循环将列表附加到字典
- php - 是否可以使用 Migrating 和 Seed Once 进行 InMemory 测试
- python - 代码不能作为函数工作,但可以在主代码中工作。(Python,pygame)
- django - Ajax - LocalHost 上的 500 内部服务器错误
- angular - 在更改事件之前以角度形式将先前选择的值存储在选择框中
- react-native - 如何使用 react-native-testing-library 提交表单或在输入上按 enter 键