I have a case where a first few command execution attempts after daemon restart are returning 2 (followed by stderr messages), than everything works correctly.

In order to bypass this bug I've come to an idea that I should utilize while/do/done/until functionality with some sleep, but the fun part is that I should have all the data I'd get by the regular execution way:

RESULT=$(command param1 param2 2>&1)

So is there a way to execute command param1 param2 repeatedly until exit status is NOT 2 (any given value) and to have STDOUT+STDERR in a variable RESULT and exit status in variable SIG, as presented?


while ! command param1 param2 >"$outfile" 2>&1; do
    [ "$?" -ne 2 ] && break
    sleep 10

rm "$outfile"

This would run the command over and over again (with a 10 second delay in-between each time), until it succeeds or at least does not return a 2 as exit status.

The output of the command is written to a temporary file which, after the loop, is read into a variable and deleted. The reading into a variable bit requires bash, the way I've written it here.

until [ "$SIG" != "2" ]; do
   command param1 param2

This will run the command every time until it returns a number different from 2.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.