From 29f6d8bd385cb0eb44b76f681fcc6688158eea52 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Fri, 26 Feb 2016 17:21:10 +1300 Subject: Add igex(1) --- README.markdown | 2 ++ bin/igex | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ man/man1/igex.1 | 20 ++++++++++++ 3 files changed, 117 insertions(+) create mode 100755 bin/igex create mode 100644 man/man1/igex.1 diff --git a/README.markdown b/README.markdown index 7411e3b3..adc791e8 100644 --- a/README.markdown +++ b/README.markdown @@ -278,6 +278,8 @@ Scripts any options, mostly useful for scripts. * `han(1)` provides a `keywordprg` for Vim's Bash script filetype that will look for `help` topics. You could use it from the shell too. +* `igex(1)` wraps around a command to allow you to ignore error conditions + that don’t actually worry you, exiting with 0 anyway. * `maybe(1)` is like `true(1)` or `false(1)`; given a probability of success, it exits with success or failure. Good for quick tests. * `sue(8)` execs `sudoedit(8)` as the owner of all the file arguments given, diff --git a/bin/igex b/bin/igex new file mode 100755 index 00000000..c4492dc3 --- /dev/null +++ b/bin/igex @@ -0,0 +1,95 @@ +#!/usr/bin/env bash + +# +# igex(1): Run a command and ignore specified exit values, translating them to +# 0. Don't touch any stderr, though. Good for wrapping around rsync(1), like to +# ignore exit value 24. +# +# -h gives help, -v gives you stdout specifying success or failure, -i specifies +# the signals to ignore; you must specify at least one none-zero integer. +# +# Author: Tom Ryder +# Copyright: 2016 +# License: Public domain +# +self=igex + +# Print usage information +usage() { + printf '%s: usage: %s [-hv] -i IGNORE1[,IGNORE2...] [--] COMMAND [ARG1...]\n' \ + "$self" "$self" +} + +# Array with exit values to ignore +declare -a ignores +ignores=() + +# Flag for whether to print diagnostics to stderr or not; defaults to off +declare -i verbose +verbose=0 + +# Process options +while getopts 'hvi:' opt ; do + case $opt in + + # -h: Print help + h) + usage + exit 0 + ;; + + # -v: Print diagnostics to stderr + v) + verbose=1 + ;; + + # Specify the comma-delimited signals to ignore + i) + IFS=, read -a ignores < <(printf '%s\n' "$OPTARG") + ;; + + # Unknown option + \?) + usage >&2 + exit 2 + ;; + esac +done +shift "$((OPTIND-1))" + +# Check we have at least one ignore value +if ! ((${#ignores[@]})) ; then + usage >&2 + exit 2 +fi + +# Check that all the ignore values are non-zero +for ignore in "${ignores[@]}" ; do + ((ignore != 0)) && continue + usage >&2 + exit 2 +done + +# Check we have some arguments left to run a command +if ! (($#)) ; then + usage >&2 + exit 2 +fi + +# Run the command and save its exit value +"$@" +ret=$? + +# Iterate through the ignored exit values and reset the exit value to 0 if it +# matches any of them, including a warning to stderr if -v was specified +for ignore in "${ignores[@]}" ; do + ((ret != ignore)) && continue + ((verbose)) && printf '%s: Ignoring exit value %u\n' \ + "$self" "$ignore" >&2 + ret=0 + break +done + +# Exit with the determined value +exit "$ret" + diff --git a/man/man1/igex.1 b/man/man1/igex.1 new file mode 100644 index 00000000..464070aa --- /dev/null +++ b/man/man1/igex.1 @@ -0,0 +1,20 @@ +.TH IGEX 1 "February 2016" "Manual page for igex" +.SH NAME +.B igex +\- run a command, ignoring specified error exit values +.SH USAGE +.B igex [-hv] -i IGNORE1[,IGNORE2...] [--] COMMAND [ARG1...] +.SH DESCRIPTION +Runs the given command and checks its exit value. If it matches any of the +ignored values given in -i, exits 0. Otherwise, exits with the command's exit +value as normal. +.P +Option -h gives help, option -v turns on verbose output, option -i specifies an +the signals to ignore, comma-delimited; you must specify at least one non-zero +integer. +.P + $ igex -i1 false + $ igex -v -i24 rsync source dest +.SH AUTHOR +Tom Ryder + -- cgit v1.2.3