Skip to content

Instantly share code, notes, and snippets.

@alexfornuto
Last active May 1, 2020 15:28
Show Gist options
  • Save alexfornuto/2d8a4834abfa1175f0d7edc10564253c to your computer and use it in GitHub Desktop.
Save alexfornuto/2d8a4834abfa1175f0d7edc10564253c to your computer and use it in GitHub Desktop.
A bash function that tries a command 3 times before failing with exit code 1
try3 () {
for ((n=1;n<4;n++)); do
if ! "$@"
then
echo "failed $n times..."
if [[ $n = 3 ]]
then exit 1
fi
sleep 1
else
echo "Completed after $n tries"
break
fi
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment