Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
509 views
in Technique[技术] by (71.8m points)

windows - 在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)?
  ask by translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...