代码之家  ›  专栏  ›  技术社区  ›  temporary_user_name

如何将前一个命令的输出顶行解析为另一个命令的输入?

  •  0
  • temporary_user_name  · 技术社区  · 6 年前

    在bash中,我将使用awk和xargs来实现这一点,但我不确定如何在Windows命令行中实现这一点。

    基本上,我正试图终止监听端口3000的进程。我现在这样做:

    netstat -ano | findstr:3000
    taskkill /PID <process id found from previous command> /F
    

    有没有一种方法可以自动解析前一个命令,将它们链接在一行中,这样我就不必一直手工输入最新的PID了?或者如果有人知道更好的方法,那也很感激。

    1 回复  |  直到 6 年前
        1
  •  1
  •   double-beep hudson solomon    6 年前

    您需要使用 for 循环如下所示:

    for /f "tokens=5" %A in ('netstat -ano ^| findstr /c:3000 ^| findstr /c:LISTENING') do taskkill /PID %A /F
    

    希望这有帮助!