Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch file: re-run command until output contains keyword

I have a batch script where I want to run a particular command (which outputs a status) until its output contains a particular keyword. I'm not sure either of how to loop like this in a batch script nor of how to save and parse the output of a command.

I've tried an approach as described in Raymond Chen's "Reading the output of a command into a batch file variable" (which uses a for loop to parse command output), without success (it's quite possible that I'm simply mangling or misunderstanding the commands). Any suggestions for how to repeat a command, parse its output, and repeat until the output is acceptable?

like image 548
Ben Klein Avatar asked Mar 12 '26 07:03

Ben Klein


1 Answers

no need to put the output into a variable for this. Just:

:loop
timeout /t 1 >nul
command | find "this is what I want"    
if errorlevel 1 goto :loop
echo successful.
like image 199
Stephan Avatar answered Mar 14 '26 06:03

Stephan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!