gpt4 book ai didi

windows - 要求用户输入(/P)时批处理脚本中的语法错误

转载 作者:行者123 更新时间:2023-12-03 08:12:07 25 4
gpt4 key购买 nike

我问了几个人问题所在,并且两次走开却没有解决方案。我之前并没有玩过很多游戏,所以这可能是一个简单的错误。目前,该代码应提供使用wmic的进程列表。最终,我想将其设置为杀死进程(我应该能够很容易地做到这一点),但是我必须首先克服这个障碍。

@echo off
set getprocesslistlocal=wmic process get name,processid
set /P remotemachinecheck=Type the name of the remote machine to view processes of (or type local for local machine), and press Enter.
if %remotemachinecheck%==local
(
%getprocesslistlocal%
) else (
set remotemachine=%remotemachinecheck%
set /P remoteuser=Type the user name to access %remotemachine% with, then press Enter.
set /P remotepassword=[Type the password for %remoteuser% on %remotemachine%, then press Enter. Watch your back, it won't be hidden!
set getprocesslistremote=wmic /node %remotemachine% /user:%remoteuser% /password:%remotepass% process get name,processid
%getprocesslistremote%
)
echo End of list. Press any key to choose process to kill.
@echo off
pause>null

最佳答案

您要做的第一件事是注释掉@echo off(或将其更改为@echo on),以便您可以准确看到引起错误的行。

您应该查看的第二件事是这样的事实,即在执行命令之前要对命令进行处理,而不是执行行。

这意味着在设置if ( ) else ( )变量之前,整个remotemachine结构都将对其进行替换,这意味着当您要使用它时,不会将其设置为所需的值。

例如,此代码:

@echo off
set xyzzy=plugh
if a==a (
set xyzzy=twisty
echo %xyzzy%
)

不会按照您的想法输出 twisty,而是提供 plugh

您需要研究延迟扩展和 !!扩展运算符:
@setlocal enableextensions enabledelayedexpansion
@echo off
set xyzzy=plugh
if a==a (
set xyzzy=twisty
echo !xyzzy!
)
endlocal

第三件事,我怀疑这是导致您直接问题的原因,将 (移动到 if行的末尾:
if %remotemachinecheck%==local (

在下一行的 (中,它会生成如下错误:
The syntax of the command is incorrect.

但这是一件棘手的事情:它会在 if行之前输出该错误,这可能会导致您认为错误是根据以下记录(缩进由计算机生成的行)在上一行中:
c:\pax> type qq1.cmd
set q=w
if a==a
(
echo yes
)

c:\pax> qq1

c>\pax> set q=w
The syntax of the command is incorrect.

c:\pax> if a==a

c:\pax> type qq2.cmd
set q=w
if a==a (
echo yes
)

c:\pax> qq2

c>\pax> set q=w

c:\pax> if a==a (echo equal )
equal

关于windows - 要求用户输入(/P)时批处理脚本中的语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16535494/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com