在 Windows cmd 中,如何提示用户输入并在另一个命令中使用结果?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1223721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:37:46  来源:igfitidea点击:

In Windows cmd, how do I prompt for user input and use the result in another command?

windowsbatch-fileprompt

提问by Instantsoup

I have a Windows .bat file which I would like to accept user input and then use the results of that input as part of the call to additional commands.

我有一个 Windows .bat 文件,我想接​​受用户输入,然后将该输入的结果用作调用其他命令的一部分。

For example, I'd like to accept a process ID from the user, and then run jstack against that ID, putting the results of the jstack call into a file. However, when I try this, it doesn't work.

例如,我想接受来自用户的进程 ID,然后针对该 ID 运行 jstack,将 jstack 调用的结果放入一个文件中。但是,当我尝试此操作时,它不起作用。

Here's my sample bat file contents:

这是我的示例 bat 文件内容:

@echo off
set /p id=Enter ID: 
echo %id%
jstack > jstack.txt

and here's what shows up in jstack.txt:

这是 jstack.txt 中显示的内容:

Enter ID: Terminate batch job (Y/N)? 

回答by Instantsoup

Try this:

尝试这个:

@echo off
set /p id="Enter ID: "

You can then use %id%as a parameter to another batch file like jstack %id%.

然后,您可以将其%id%用作另一个批处理文件的参数,例如jstack %id%.

For example:

例如:

set /P id=Enter id: 
jstack %id% > jstack.txt

回答by jayfessenden

The syntax is as such: set /p variable=[string]

语法如下: set /p variable=[string]

Check out http://commandwindows.com/batch.htmor http://www.robvanderwoude.com/userinput.phpfor a more deep dive into user input with the different versions of Windows OS batch files.

查看http://commandwindows.com/batch.htmhttp://www.robvanderwoude.com/userinput.php以更深入地了解不同版本的 Windows 操作系统批处理文件的用户输入。

Once you have set your variable, you can then go about using it in the following fashion.

一旦您设置了变量,您就可以按照以下方式使用它。

@echo off
set /p UserInputPath=What Directory would you like?
cd C:\%UserInputPath%

note the %VariableName%syntax

注意%VariableName%语法

回答by Daniel

I am not sure if this is the case for all versions of Windows, however on the XP machine I have, I need to use the following:

我不确定所有版本的 Windows 是否都是这种情况,但是在我拥有的 XP 机器上,我需要使用以下内容:

set /p Var1="Prompt String"

Without the prompt string in quotes, I get various results depending on the text.

如果没有引号中的提示字符串,我会根据文本得到各种结果。

回答by hannad rehman

set /p choice= "Please Select one of the above options :" 
echo '%choice%'

The space after =is very important.

后面的空间=很重要。

回答by fedeb

@echo off
set /p input="Write something, it will be used in the command "echo""
echo %input%
pause

if i get what you want, this works fine. you can use %input% in other commands too.

如果我得到你想要的东西,这很好用。您也可以在其他命令中使用 %input% 。

@echo off
echo Write something, it will be used in the command "echo"
set /p input=""
cls
echo %input%
pause 

回答by Timothy J. McGowan

There is no documented /prompt parameter for SETX as there is for SET.

SETX 没有记录的 /prompt 参数,因为 SET 有。

If you need to prompt for an environment variable that will survive reboots, you can use SETX to store it.

如果您需要提示输入一个可以在重新启动后继续存在的环境变量,您可以使用 SETX 来存储它。

A variable created by SETX won't be usable until you restart the command prompt. Neatly, however, you can SETX a variable that has already been SET, even if it has the same name.

在您重新启动命令提示符之前,由 SETX 创建的变量将不可用。然而,您可以设置一个已经设置的变量,即使它具有相同的名称。

This works for me in Windows 8.1 Pro:

这在 Windows 8.1 Pro 中对我有用:

set /p UserInfo= "What is your name?  "
setx UserInfo "%UserInfo%"

(The quotation marks around the existing variable are necessary.)

(现有变量周围的引号是必需的。)

This procedure allows you to use the temporary SET-created variable during the current session and will allow you to reuse the SETX-created variable upon reboot of the computer or restart of the CMD prompt.

此过程允许您在当前会话期间使用由 SET 创建的临时变量,并允许您在重新启动计算机或重新启动 CMD 提示符时重用由 SETX 创建的变量。

(Edited to format code paragraphs properly.)

(编辑以正确格式化代码段落。)

回答by Anisha Gupta

@echo off
:start
set /p var1="Enter first number: "
pause

回答by Werdna

I have a little cmd I use when preparing pc to clients: it calls the user for input, and the rename the pc to that.

我在为客户端准备 pc 时使用了一些 cmd:它调用用户进行输入,然后将 pc 重命名为该名称。

@ECHO "remember to run this as admin."
@ECHO OFF
SET /P _inputname= Please enter an computername:
@ECHO Du intastede "%_inputname%"
@ECHO "The pc will restart after this"
pause
@ECHO OFF
wmic computersystem where name="%COMPUTERNAME%" call rename name="%_inputname%"

shutdown -r -f

回答by pashute

There are two possibilities.

有两种可能。

  1. You forgot to put the %id%in the jstackcall.

    jstack %id% > jstack.txt
    
  1. 你忘了把%id%jstack电话。

    jstack %id% > jstack.txt
    

So the whole correct batch file should be:

所以整个正确的批处理文件应该是:

@echo off
set /p id=Enter ID: 
echo %id%
jstack %id% > jstack.txt

And/Or 2. You did put it in the code (and forgot to tell us in the question) but when you ran the batch file you hit the Enter key instead of typing an ID (say 1234).

和/或 2. 您确实将其放入代码中(并且忘记在问题中告诉我们),但是当您运行批处理文件时,您按 Enter 键而不是输入 ID(例如 1234)。

What's happening is the result of these two mistakes: jstackis supposed to be called with the id that you supply it.

发生的事情是这两个错误的结果: jstack应该使用您提供的 id 调用。

But in your case (according to the code you supplied in the question) you called it without any variable. You wrote:

但是在您的情况下(根据您在问题中提供的代码),您在没有任何变量的情况下调用了它。你写了:

jstack > jstack.txt

So when you run jstackwith no variable it outputs the following:

因此,当您在jstack没有变量的情况下运行时,它会输出以下内容:

Terminate batch file Y/N? 

Your second mistake is that you pressed Enter instead of giving a value when the program asked you: Enter ID:. If you would have put in an ID at this point, say 1234, the %id%variable would become that value, in our case 1234. But you did NOT supply a value and instead pressed Enter. When you don't give the variable any value, and if that variable was not set to anything else before, then the variable %id%is set to the prompt of the setcommand!! So now %id%is set to Enter ID:which was echoed on your screen as requested in the batch file BEFORE you called the jstack.

你的第二个错误是当程序询问你时你按下了 Enter 而不是给出一个值: Enter ID:。如果您此时输入 ID,例如 1234,则%id%变量将成为该值,在我们的示例中为 1234。但是您没有提供值而是按 Enter。当你不给变量任何值时,如果该变量之前没有设置为其他任何值,那么该变量%id%将设置为set命令的提示!!所以现在%id%设置为Enter ID:在您调用 jstack 之前批处理文件中的要求在您的屏幕上回显的那个。

But I suspect you DID have the jstack %id% > jstack.txtin your batch file code with the %id(and omitted it by mistake from the question), and that you hit enter without typing in an id. The batch program then echoed the id, which is now "Enter ID:", and then ran jstack Enter ID: > jstack.txt

但我怀疑你jstack %id% > jstack.txt的批处理文件代码中%id确实有(并在问题中错误地省略了它),并且你在没有输入 ID 的情况下点击了回车。批处理程序然后回显id,现在是“输入ID:”,然后运行jstack Enter ID: > jstack.txt

Jstack itself echoed the input, encountered a mistake and asked to terminate.
And all this was written into the jstack.txt file.

Jstack 本身回显了输入,遇到错误并要求终止。
所有这些都被写入到 jstack.txt 文件中。

回答by SRE

Dollar signs around the variable do not work on my Vista machine, but percent signs do. Also note that a trailing space on the "set" line will show up between the prompt and user input.

变量周围的美元符号在我的 Vista 机器上不起作用,但百分号可以。另请注意,“set”行上的尾随空格将显示在提示和用户输入之间。