简体   繁体   English

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

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

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.例如,我想接受来自用户的进程 ID,然后针对该 ID 运行 jstack,将 jstack 调用的结果放入文件中。 However, when I try this, it doesn't work.但是,当我尝试这个时,它不起作用。

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)?

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

The syntax is as such: set /p variable=[string]语法如下: set /p variable=[string]

Check out http://commandwindows.com/batch.htm or http://www.robvanderwoude.com/userinput.php for 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%语法

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

The space after = is very important. =后面的空格很重要。

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.如果没有引号中的提示字符串,我会根据文本得到各种结果。

@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 

There is no documented /prompt parameter for SETX as there is for SET. SETX 没有像 SET 那样记录在案的 /prompt 参数。

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.在您重新启动命令提示符之前,SETX 创建的变量将无法使用。 Neatly, however, you can SETX a variable that has already been SET, even if it has the same name.然而,巧妙地,您可以对已设置的变量进行设置,即使它具有相同的名称。

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.) (编辑以正确格式化代码段落。)

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

You can try also with userInput.bat which uses the html input element.您也可以尝试使用使用 html 输入元素的userInput.bat

在此处输入图像描述

This will assign the input to the value jstackId:这会将输入分配给值 jstackId:

call userInput.bat jstackId
echo %jstackId%

This will just print the input value which eventually you can capture with FOR /F :这只会打印最终您可以使用FOR /F捕获的输入值:

call userInput.bat

There are two possibilities.有两种可能性。

  1. You forgot to put the %id% in the jstack call.您忘记将%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: jstack is supposed to be called with the id that you supply it.发生的事情是这两个错误的结果:应该使用您提供的 id 调用jstack

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 jstack with 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: .您的第二个错误是当程序询问您时您按 Enter 而不是给出值: 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.如果此时您输入了一个 ID,例如 1234, %id%变量将变为该值,在我们的示例中为 1234。但是您没有提供值,而是按 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 set command!!当您不给变量任何值,并且如果该变量之前没有设置为其他任何值,那么变量%id%将设置为set命令的提示符!! 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.所以现在%id%设置为Enter ID:在您调用 jstack 之前,它已按照批处理文件中的要求在屏幕上回显。

But I suspect you DID have the jstack %id% > jstack.txt in 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.但是我怀疑您在批处理文件代码中确实有jstack %id% > jstack.txt%id (并且在问题中错误地省略了它),并且您在没有输入 id 的情况下按了输入。 The batch program then echoed the id, which is now "Enter ID:", and then ran jstack Enter ID: > jstack.txt然后批处理程序回显 id,现在是“输入 ID:”,然后运行jstack Enter ID: > jstack.txt

Jstack itself echoed the input, encountered a mistake and asked to terminate. Jstack 本身回应了输入,遇到错误并要求终止。
And all this was written into the jstack.txt file.而这一切都被写入了 jstack.txt 文件。

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

Dollar signs around the variable do not work on my Vista machine, but percent signs do.变量周围的美元符号在我的 Vista 机器上不起作用,但百分号可以。 Also note that a trailing space on the "set" line will show up between the prompt and user input.另请注意,“set”行上的尾随空格将显示在提示符和用户输入之间。

Just added the刚刚添加了

set /p NetworkLocation= Enter name for network?

echo %NetworkLocation% >> netlist.txt

sequence to my netsh batch job.序列到我的 netsh 批处理作业。 It now shows me the location I respond as the point for that sample.它现在向我显示我响应的位置作为该样本的点。 I continuously >> the output file so I know now "home", "work", "Starbucks", etc. Looking for clear air, I can eavulate the lowest use channels and whether there are 5 or just all 2.4 MHz WLANs around.我不断地>>输出文件,所以我现在知道“家”、“工作”、“星巴克”等。寻找干净的空气,我可以评估最低使用频道以及周围是否有 5 个或只有所有 2.4 MHz WLAN。

Just to keep a default value of the variable.只是为了保持变量的默认值。 Press Enter to use default from the recent run of your .bat:按 Enter 使用最近运行的 .bat 中的默认值:

@echo off
set /p Var1=<Var1.txt
set /p Var1="Enter new value ("%Var1%") "
echo %Var1%> Var1.txt

rem YourApp %Var1%

In the first run just ignore the message about lack of file with the initial value of the variable (or do create the Var1.txt manually).在第一次运行时,只需忽略有关缺少具有变量初始值的文件的消息(或手动创建 Var1.txt)。

One other way which might be interesting.另一种可能很有趣的方式。 You can call a powershell script from where you can do pretty much anything, and send the data bach to cmd or do other stuff with something like this.您可以调用一个 powershell 脚本,您可以在其中执行几乎任何操作,并将数据 bach 发送到 cmd 或使用类似的东西执行其他操作。

set var=myvar; 
call "c:\input2.cmd" %var%. 

Its kind of more flexible (You can send the data the same way with powershell).它更灵活(您可以使用 powershell 以相同的方式发送数据)。

So in your cmd, write this considering the ps script is in C: :因此,在您的 cmd 中,考虑到 ps 脚本位于C:中,请编写此代码:

PowerShell.exe -ExecutionPolicy unrestricted -Command "& {. C:\Input.ps1}"

And in your input.ps1 script, write this:在你的 input.ps1 脚本中,这样写:

$var = Read-Host -Prompt "Your input"
Write-Host "Your var:",$var
#Do stuff with your variable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM