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

发布于 2024-07-30 05:07:46 字数 347 浏览 5 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

偏闹i 2024-08-06 05:07:46

试试这个:

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

然后,您可以使用 %id% 作为另一个批处理文件(如 jstack %id%)的参数。

例如:

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

Try this:

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

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

For example:

set /P id=Enter id: 
jstack %id% > jstack.txt
青丝拂面 2024-08-06 05:07:46

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

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

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

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

注意 %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.

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

note the %VariableName% syntax

七月上 2024-08-06 05:07:46
set /p choice= "Please Select one of the above options :" 
echo '%choice%'

= 后面的空格非常重要。

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

The space after = is very important.

嗫嚅 2024-08-06 05:07:46

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

set /p Var1="Prompt String"

如果没有 引号 中的提示字符串,我会得到各种结果,具体取决于文本。

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:

set /p Var1="Prompt String"

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

何时共饮酒 2024-08-06 05:07:46
@echo off
set /p input="Write something, it will be used in the command "echo""
echo %input%
pause

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

@echo off
echo Write something, it will be used in the command "echo"
set /p input=""
cls
echo %input%
pause 
@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.

@echo off
echo Write something, it will be used in the command "echo"
set /p input=""
cls
echo %input%
pause 
鹿港巷口少年归 2024-08-06 05:07:46

您也可以尝试使用 userInput.bat< /a> 使用 html 输入元素。

输入图像描述这里

这会将输入分配给值 jstackId:

call userInput.bat jstackId
echo %jstackId%

这将只打印输入值,最终您可以使用 FOR /F 捕获该值:

call userInput.bat

You can try also with userInput.bat which uses the html input element.

enter image description here

This will assign the input to the value jstackId:

call userInput.bat jstackId
echo %jstackId%

This will just print the input value which eventually you can capture with FOR /F :

call userInput.bat
狼亦尘 2024-08-06 05:07:46

与 SET 不同,SETX 没有记录 /prompt 参数。

如果您需要提示输入重新启动后仍有效的环境变量,可以使用 SETX 来存储它。

在重新启动命令提示符之前,SETX 创建的变量将无法使用。 然而,您可以巧妙地 SETX 一个已被 SET 的变量,即使它具有相同的名称。

这在 Windows 8.1 Pro 中适用于我:(

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

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

此过程允许您在当前会话期间使用 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:

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.

(Edited to format code paragraphs properly.)

笑,眼淚并存 2024-08-06 05:07:46
@echo off
:start
set /p var1="Enter first number: "
pause
@echo off
:start
set /p var1="Enter first number: "
pause
怪我太投入 2024-08-06 05:07:46

有两种可能性。

  1. 您忘记将 %id% 放入 jstack 调用中。

    jstack %id% >   jstack.txt 
      

因此,整个正确的批处理文件应该是:

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

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

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

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

jstack > jstack.txt

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

Terminate batch file Y/N? 

您的第二个错误是当程序要求您输入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.

  1. You forgot to put the %id% in the jstack call.

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

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:

jstack > jstack.txt

So when you run jstack with no variable it outputs the following:

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 set command!! 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.

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 ran 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.

如梦 2024-08-06 05:07:46

我在为客户端准备电脑时使用了一个小cmd:它调用用户进行输入,并将电脑重命名为该输入。

@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

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.

@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
肤浅与狂妄 2024-08-06 05:07:46

变量周围的美元符号在我的 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.

陌上芳菲 2024-08-06 05:07:46

刚刚将

set /p NetworkLocation= Enter name for network?

echo %NetworkLocation% >> netlist.txt

序列添加到我的 netsh 批处理作业中。 现在它显示了我作为该样本点响应的位置。 我不断>> 输出文件,这样我现在就知道“家”、“工作”、“星巴克”等。寻找晴朗的空气,我可以评估最低使用频道以及周围是否有 5 个或全部 2.4 MHz WLAN。

Just added the

set /p NetworkLocation= Enter name for network?

echo %NetworkLocation% >> netlist.txt

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.

忘你却要生生世世 2024-08-06 05:07:46

只是为了保留变量的默认值。 按 Enter 键使用最近运行的 .bat 中的默认值:

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

rem YourApp %Var1%

在第一次运行中,只需忽略有关缺少带有变量初始值的文件的消息(或者手动创建 Var1.txt)。

Just to keep a default value of the variable. Press Enter to use default from the recent run of your .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).

万水千山粽是情ミ 2024-08-06 05:07:46

另一种可能有趣的方式。 您可以调用一个 powershell 脚本,您可以在其中执行几乎任何操作,并将数据发送到 cmd 或使用类似的方法执行其他操作。

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

它更灵活(您可以使用 powershell 以相同的方式发送数据)。

因此,在您的 cmd 中,考虑到 ps 脚本位于 C: 中,请编写以下内容:

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

在您的 input.ps1 脚本中,编写以下内容:

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

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.

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

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::

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

And in your input.ps1 script, write this:

$var = Read-Host -Prompt "Your input"
Write-Host "Your var:",$var
#Do stuff with your variable
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文