Wednesday, April 27, 2022

[SOLVED] Repeatedly run a shell command until it fails?

Issue

I've written a fuzzy test that fails unreliably. I've added some debug code, but now I want to run the test until it fails so I can gather the debug output.

I've setup the test so I can run it using:

./runtest

My current solution is to write an untilfail script:

#!/bin/bash
$@
while [ $? -eq 0 ]; do
    $@
done

Then use it:

untilfail ./runtest

Is there a simpler solution?


Solution

while takes a command to execute, so you can use the simpler

while ./runtest; do :; done

This will stop the loop when ./runtest returns a nonzero exit code (which is usually indicative of failure).

To further simplify your current solution though, you should just change your untilfail script to look like this:

#!/bin/bash

while "$@"; do :; done

And then you can call it with whatever command you're already using:

untilfail ./runTest --and val1,val2 -o option1 "argument two"


Answered By - nneonneo
Answer Checked By - Mildred Charles (WPSolving Admin)