在窗口 bash 中处理命令响应



我正在编写一个应该重新启动正在运行的进程的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:PandoraPCM.exe

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

TL;博士-

使用set命令

详:

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
)

最新更新