用可变值中的空格设置/p [英] set /p with spaces in variable value

查看:63
本文介绍了用可变值中的空格设置/p的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想批量制作一个基本的文本编辑器,我正在使用set/p获取用户输入并将其写入文件.

I wanted to make a basic text editor in batch and I'm using set /p to get user input and write that to a file.

当我输入"hello"或"hello"之类的东西时,它可以正常工作,但是当我在其后写有空格和另一个字符的东西时,例如"hello world",则窗口将关闭.

When I type in something like "hello" or "hello " it works fine, but as soon as I write something with a space and another character after than, like "hello world", the window closes.

我的代码是:

set /p append=Write what you want to append: 

然后我尝试:

set /p "append=Write what you want to append: "

我在网上找到的

,但是没有用.

which i found online, but it didn't work.

暂停没有任何帮助,因为一旦按下Enter键,程序就会崩溃.

pause isn't of any help because the program crashes as soon as I press enter.

帮助!

这是完整的代码:

:append
set /p "append=Write what you want to append: "
if %append%==return (
    cls
    goto start
)
echo %append% >> %filename%
goto append

:override
cls
echo Name: %filename%
set /p override="Write new content: "
echo %override% > %filename%
pause
cls
goto start

:writefile
echo.
set /p filename=Write file name.extension: 
choice /c AO /m "Append to file or override? "
if %ERRORLEVEL%==1 (
    cls
    echo Name: %filename%
    goto append
) else (
    goto override
)

推荐答案

每当使用set /P提示批处理文件的用户输入字符串时,该用户都可以自由

Whenever the user of a batch file is prompted for a string using set /P the user has the freedom to

    只需按 RETURN ENTER 根本不输入任何内容,这会导致在提示之前仍未定义环境变量,或者该变量之前仍未定义保持其当前值(如果之前已经定义过),或者
  1. 输入任何内容,包括字符串,这可能会导致批处理执行退出,这是因为稍后批处理代码中的某个语法错误或不希望有的行为.
  1. enter nothing at all by pressing simply just RETURN or ENTER which results in the environment variable is still not defined after prompt if it was not defined before, or the variable keeps its current value if it was already defined before, or
  2. enter really anything including a string which could result in an exit of batch execution because of a syntax error somewhere in batch code later or an unwanted behavior.

让我们拭目以待

if %append%==return (

如果之前未使用默认值定义环境变量,并且批处理用户未输入任何内容,则该行在预处理步骤中扩展为:

If environment variable is not defined before with a default value and the batch user enters nothing, this line expands during preprocessing step to:

if ==return (

此命令行当然是无效的,并且由于语法错误而导致退出批处理.

This command line is of course invalid and results in an exit of batch processing because of a syntax error.

现在让我们假设用户输入了字符串:

Now let us assume the user enters the string:

I want to append this string.

然后 IF 命令行扩展为:

if I want to append this string.==return (

当然,此命令行也是无效的.

And of course also this command line is invalid.

其他回答者建议将两个字符串括起来以用双引号进行比较,即使用命令行:

The other answerers suggested to enclose the two strings to compare in double quotes, i.e. use the command line:

if "%append%"=="return" (

但这真的可以解决所有可能的问题吗?

But does that really solve all possible issues?

该代码现在真的可以安全地抵抗任何用户输入吗?

Is the code now really safe against any user input.

让我们看看如果用户输入字符串会发生什么情况:

Let us look what happens if the user enters the string:

" is a double quote. It should be used around paths like "%ProgramFiles(x86)%".

IF 命令行现在扩展为:

if "" is a double quote. It should be used around paths like "%ProgramFiles(x86)%""=="return" (

此命令行再次无效,由于语法错误,导致批处理文件退出.

And this command line is again invalid and results in an exit of the batch file because of a syntax error.

那么如何使批处理代码真正免受任何用户输入的侵害?

So how to make batch code safe against really any user input?

该解决方案是在引用用户输入的字符串的任何地方使用延迟的环境变量扩展.

The solution is using delayed environment variable expansion wherever the user entered string is referenced.

示例:

@echo off
setlocal EnableDelayedExpansion
echo.
echo Enter EXIT to exit this batch script.

:PromptUser
echo.
set "UserInput=Nothing^!"
set /P "UserInput=Please enter a string: "
if /I "!UserInput!" == "exit" goto EndBatch
echo You entered: !UserInput!
goto PromptUser

:EndBatch
echo.
echo Thanks for running this example batch code.
echo.
echo The batch file ends in 3 seconds ...
endlocal
%SystemRoot%\System32\ping.exe localhost -n 4 >nul

通过使用延迟扩展,用户输入的字符串不会在以后的代码中导致无效或意外的命令行.这对于命令行也很重要

By using delayed expansion the user entered string can't result in a invalid or unexpected command line in later code. This is also important for the command line

echo !append!>>%filename%

重要的是,必须在>>后面没有空格字符,否则此空格字符也将作为尾随空格写入文件中.

It is important to have no space character left of >> or this space character is also written into the file as trailing space.

但是在这种情况下,延迟扩展在用户输入例如2的情况下也很重要,这会导致echo %append%>>%filename% in

But delayed expansion is also important here in case of user enters for example just 2 which would result with echo %append%>>%filename% in

echo 2>>%filename%

不会将字符2附加到文件中,而是将 STDERR 附加到文件中,从而导致将空行写入文件.

which does not append the character 2 to the file, but would append STDERR to the file which results in an empty line written to the file.

用户输入的此字符串还需要延迟扩展:

Delayed expansion is also needed for this string entered by the user:

(Var1 > 0x2F) && (Var1 < 0x3A)

应使用 ECHO 写入输入的文件中,而不是使用echo %append%>>%filename%展开字符串后Windows命令解释器将生成的内容.

which should be written with ECHO into the file as entered and not what Windows command interpreter would produce after expanding the string when using echo %append%>>%filename%.

这篇关于用可变值中的空格设置/p的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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