在 Windows cmd 中,如何提示用户输入并在另一个命令中使用结果?
我有一个 Windows .bat 文件,我想接受用户输入,然后使用该输入的结果作为调用其他命令的一部分。
例如,我想接受用户的进程 ID,然后针对该 ID 运行 jstack,将 jstack 调用的结果放入文件中。 但是,当我尝试这样做时,它不起作用。
这是我的示例 bat 文件内容:
@echo off
set /p id=Enter ID:
echo %id%
jstack > jstack.txt
这是 jstack.txt 中显示的内容:
Enter ID: Terminate batch job (Y/N)?
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.
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.
Here's my sample bat file contents:
@echo off
set /p id=Enter ID:
echo %id%
jstack > jstack.txt
and here's what shows up in jstack.txt:
Enter ID: Terminate batch job (Y/N)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
试试这个:
然后,您可以使用
%id%
作为另一个批处理文件(如jstack %id%
)的参数。例如:
Try this:
You can then use
%id%
as a parameter to another batch file likejstack %id%
.For example:
语法如下:
set /p variable=[string]
查看 http:// commandwindows.com/batch.htm 或 http://www.robvanderwoude.com/userinput.php 更深入地了解不同版本的 Windows 操作系统批处理文件的用户输入。
设置变量后,您就可以按照以下方式使用它。
注意
%VariableName%
语法The syntax is as such:
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.
Once you have set your variable, you can then go about using it in the following fashion.
note the
%VariableName%
syntax=
后面的空格非常重要。The space after
=
is very important.我不确定所有版本的 Windows 是否都是这种情况,但是在我拥有的 XP 计算机上,我需要使用以下内容:
如果没有 引号 中的提示字符串,我会得到各种结果,具体取决于文本。
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:
Without the prompt string in quotes, I get various results depending on the text.
如果我得到你想要的,那就很好了。 您也可以在其他命令中使用%input%。
if i get what you want, this works fine. you can use %input% in other commands too.
您也可以尝试使用 userInput.bat< /a> 使用 html 输入元素。
这会将输入分配给值 jstackId:
这将只打印输入值,最终您可以使用
FOR /F
捕获该值:You can try also with userInput.bat which uses the html input element.
This will assign the input to the value jstackId:
This will just print the input value which eventually you can capture with
FOR /F
:与 SET 不同,SETX 没有记录 /prompt 参数。
如果您需要提示输入重新启动后仍有效的环境变量,可以使用 SETX 来存储它。
在重新启动命令提示符之前,SETX 创建的变量将无法使用。 然而,您可以巧妙地 SETX 一个已被 SET 的变量,即使它具有相同的名称。
这在 Windows 8.1 Pro 中适用于我:(
现有变量周围的引号是必要的。)
此过程允许您在当前会话期间使用 SET 创建的临时变量,并允许您在重新启动时重用 SETX 创建的变量计算机或重新启动 CMD 提示符。
(编辑以正确格式化代码段落。)
There is no documented /prompt parameter for SETX as there is for SET.
If you need to prompt for an environment variable that will survive reboots, you can use SETX to store it.
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.
This works for me in Windows 8.1 Pro:
(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.
(Edited to format code paragraphs properly.)
有两种可能性。
您忘记将
%id%
放入jstack
调用中。因此,整个正确的批处理文件应该是:
和/或 2.您确实将其放入代码中(并且忘记在问题中告诉我们),但是当您运行批处理文件时,您按 Enter 键而不是输入 ID(例如第1234章)
发生的事情是这两个错误的结果:
jstack
应该使用您提供的 id 进行调用。但在你的情况下(根据你在问题中提供的代码)你在没有任何变量的情况下调用了它。 您写道:
因此,当您在没有变量的情况下运行
jstack
时,它会输出以下内容:您的第二个错误是当程序要求您输入ID:
Enter ID:. 如果您此时输入一个 ID(例如 1234),则
%id%
变量将成为该值(在我们的示例中为 1234)。但是您没有提供值,而是按 Enter 键。 当您没有给变量赋予任何值,并且该变量之前未设置为其他任何值时,变量%id%
将设置为set
的提示> 命令!! 因此,现在%id%
设置为Enter ID:
,在调用 jstack 之前,它会根据批处理文件中的请求在屏幕上回显。但我怀疑你确实有
jstack %id% > jstack.txt
在批处理文件代码中使用%id
(并在问题中错误地省略了它),并且您按 Enter 键而不输入 id。 然后批处理程序回显 id,现在是“Enter ID:”,然后运行 jstack Enter ID: > jstack.txt
Jstack本身回显输入,遇到错误并要求终止。
而这一切都被写入了jstack.txt文件中。
There are two possibilities.
You forgot to put the
%id%
in thejstack
call.So the whole correct batch file should be:
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).
What's happening is the result of these two mistakes:
jstack
is supposed to be called with the id that you supply it.But in your case (according to the code you supplied in the question) you called it without any variable. You wrote:
So when you run
jstack
with no variable it outputs the following: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 theset
command!! So now%id%
is set toEnter ID:
which was echoed on your screen as requested in the batch file BEFORE you called the 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. The batch program then echoed the id, which is now "Enter ID:", and then ranjstack 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.
我在为客户端准备电脑时使用了一个小cmd:它调用用户进行输入,并将电脑重命名为该输入。
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.
变量周围的美元符号在我的 Vista 机器上不起作用,但百分号可以。
另请注意,“set”行上的尾随空格将显示在提示和用户输入之间。
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.
刚刚将
序列添加到我的 netsh 批处理作业中。 现在它显示了我作为该样本点响应的位置。 我不断>> 输出文件,这样我现在就知道“家”、“工作”、“星巴克”等。寻找晴朗的空气,我可以评估最低使用频道以及周围是否有 5 个或全部 2.4 MHz WLAN。
Just added the
sequence to my netsh batch job. 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.
只是为了保留变量的默认值。 按 Enter 键使用最近运行的 .bat 中的默认值:
在第一次运行中,只需忽略有关缺少带有变量初始值的文件的消息(或者手动创建 Var1.txt)。
Just to keep a default value of the variable. Press Enter to use default from the recent run of your .bat:
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).
另一种可能有趣的方式。 您可以调用一个 powershell 脚本,您可以在其中执行几乎任何操作,并将数据发送到 cmd 或使用类似的方法执行其他操作。
它更灵活(您可以使用 powershell 以相同的方式发送数据)。
因此,在您的 cmd 中,考虑到 ps 脚本位于 C: 中,请编写以下内容:
在您的 input.ps1 脚本中,编写以下内容:
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.
Its kind of more flexible (You can send the data the same way with powershell).
So in your cmd, write this considering the ps script is in
C:
:And in your input.ps1 script, write this: