TRY_REPEAT(1) General Commands Manual TRY_REPEAT(1)

try_repeatRun a command 'n' times, exiting early if the command returns non-zero

try_repeat [-v] count command [options ...]

try_repeat tries to run command at most count times, exiting early if command exits with a non-zero exit code. This is useful when trying to find intermittent failures in command.

The options are as follows:

Print out the iteration number before executing command.

try_repeat exits with the exit code of the last iteration of command.

$ try_repeat 3 ls /etc/motd
/etc/motd
/etc/motd
/etc/motd
$ echo $?
0
$ try_repeat 3 ls /doesntexist
ls: /doesntexist: such file or directory
$ echo $?
1
$ try_repeat -v 3 ls /etc/motd
===> 1: ls /etc/motd
/etc/motd
===> 2: ls /etc/motd
/etc/motd
===> 3: ls /etc/motd
/etc/motd

try_repeat was written by Laurence Tratt https://tratt.net/laurie/.

January 25, 2023 OpenBSD 7.6