Running a command until it succeeds
When using your shell for everyday tasks, there will be cases where a command might succeed only after some conditions are met, or the operation depends on an external event (such as a file being available to download). In such cases, one might want to run a command repeatedly until it succeeds.
How to do it...
Define a function in the following way:
repeat() { while true do $@ && return done }
Or, add this to your shell's rc
file for ease of use:
repeat() { while true; do $@ && return; done }
How it works...
We create a function called repeat
that has an infinite while
loop, which attempts to run the command passed as a parameter (accessed by $@
) to the function. It then returns if the command was successful, thereby ...
Get Linux Shell Scripting Cookbook - Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.