aboutsummaryrefslogtreecommitdiff
path: root/bin/try
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2016-08-16 18:38:49 +1200
committerTom Ryder <tom@sanctum.geek.nz>2016-08-16 18:38:49 +1200
commit0b1c99ffb1c3019e8afb9dcc542fbc5e868baef1 (patch)
tree9712aa161c25232e0f6561ee181b82eb42af347a /bin/try
parentPut "all" subtargets on their own line (diff)
downloaddotfiles-0b1c99ffb1c3019e8afb9dcc542fbc5e868baef1.tar.gz
dotfiles-0b1c99ffb1c3019e8afb9dcc542fbc5e868baef1.zip
Template traps in scripts depending on mktd(1)
Diffstat (limited to 'bin/try')
-rwxr-xr-xbin/try77
1 files changed, 0 insertions, 77 deletions
diff --git a/bin/try b/bin/try
deleted file mode 100755
index f0ba317d..00000000
--- a/bin/try
+++ /dev/null
@@ -1,77 +0,0 @@
-#!/bin/sh
-# Attempt a certain number of times to perform a task, buffer stderr unless and
-# until all command attempts fail
-self=try
-
-# Parse options
-while getopts 's:n:' opt ; do
- case $opt in
- n)
- attn=$OPTARG
- ;;
- s)
- sleep=$OPTARG
- ;;
- \?)
- printf >&2 '%s: Unknown option\n' "$self"
- exit 2
- ;;
- esac
-done
-shift "$((OPTIND-1))"
-
-# Check we have at least one argument left (the command to run)
-if [ "$#" -eq 0 ] ; then
- printf >&2 '%s: Need a command to run\n' "$self"
- exit 2
-fi
-
-# Create a buffer file for the error output, and clean up the file when we exit
-td=
-cleanup() {
- [ -n "$td" ] && rm -fr -- "$td"
- if [ "$1" != EXIT ] ; then
- trap - "$1"
- kill "-$1" "$$"
- fi
-}
-for sig in EXIT HUP INT TERM ; do
- # shellcheck disable=SC2064
- trap "cleanup $sig" "$sig"
-done
-td=$(mktd "$self") || exit
-errbuff=$td/errbuff
-
-# Open a filehandle to the error buffer, just to save on file operations
-exec 3>"$errbuff"
-
-# Keep trying the command, writing error output to the buffer file, and exit
-# if we succeed on any of them
-attc=1
-: "${attn:=3}" "${sleep:=0}"
-while [ "$attc" -le "$attn" ] ; do
-
- # Try running the command; if it succeeds, we're done, and any previous
- # failures get their errors discarded
- if "$@" 2>&3 ; then
- exit
-
- # If the command failed, record the exit value
- else
- ex=$?
- fi
-
- # If this isn't the last run, have a sleep
- if [ "$attc" -lt "$attn" ] ; then
- sleep "${sleep:=0}"
- fi
-
- # Increment the attempt count
- attc=$((attc + 1))
-done
-
-# Attempts were exhausted, and all failed; print the error output from all of
-# the failures and exit with the non-zero exit value of the most recent one
-exec 3>&-
-cat -- "$td"/errbuff >&2
-exit "$ex"