Last active
August 29, 2015 14:19
-
-
Save neg3ntropy/2396770e752319e984d8 to your computer and use it in GitHub Desktop.
Command decoration along the $PATH
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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