Skip to content

Instantly share code, notes, and snippets.

@neg3ntropy
Last active August 29, 2015 14:19
Show Gist options
  • Save neg3ntropy/2396770e752319e984d8 to your computer and use it in GitHub Desktop.
Save neg3ntropy/2396770e752319e984d8 to your computer and use it in GitHub Desktop.
Command decoration along the $PATH
#!/bin/bash
# an hypothetical /usr/local/bin/example intercepts invocations to /usr/bin/example
export SPECIAL_VAR="customize enviroments for all invocations"
do_something_before
# now invoke the same command somewhere down the $PATH (e.g. /usr/bin/example),
unwrap "$0" "${@}" --add-an-option
do_something_after # if not necessary use "exec" in the previous command
#!/bin/bash -euf
function unwrap {
export PATH="$(echo "$PATH" | sed -e "s:$(dirname "$(which "$1")")\:::" )" \
which "$(basename $1)" 2>/dev/null
}
function main {
if [ -z "$@" ]; then
echo "Usage: $0 <command> [<args>]"
exit 1
fi
local cmd="$(unwrap "$1" || (
echo "WARNING: '$1' is not wrapped" >&2
echo "$1"
))"
exec "$cmd" "${@:2}"
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment