在windows bash中处理命令响应

问题描述 投票:0回答:1

我正在编写一个重启正在运行的进程的bash脚本。我可以使用进程名称(pcm.exe)终止进程。但是,当我想要启动该过程时,我希望它从早期运行的进程中获取pcm.exe位置。这是因为我不确切地知道程序在不同系统上的位置。

我有以下脚本:

wmic process where "name='pcm.exe'" get ExecutablePath /FORMAT:LIST
@taskkill /f /im pcm.exe >nul
@timeout /t 10 /nobreak >nul
@start h:/pandora/pcm.exe >nul

wmic成功获取PCM位置:

ExecutablePath=H:\Pandora\PCM.exe

但是如何将响应传递给字符串并运行@start(路径)?

windows wmic
1个回答
0
投票

TL;DR -

使用set命令

Details:

setlocal EnableDelayedExpansion

set "zTargetImage=notepad.exe"
set "zBinLocation=0"

for /f "skip=2 tokens=2 delims=," %%A in ('wmic process where "name='!zTargetImage!'" get ExecutablePath^,ThreadCount /format:csv 2^>^&1') do (
    set "zBinLocation=%%A"
)

taskkill /f /im !zTargetImage! >nul 2>&1
timeout /t 2 /nobreak >nul
if not "!zBinLocation!"=="0" (
    start "" "!zBinLocation!" >nul
)
© www.soinside.com 2019 - 2024. All rights reserved.