Created
May 6, 2013 04:15
-
-
Save artnez/5523319 to your computer and use it in GitHub Desktop.
Monkey patching a function in bash.
This file contains hidden or 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
#! /usr/bin/env bash | |
# | |
# Monkey patching a function in bash. Why. | |
patch() { | |
new_guy=$(declare -f $1) | |
new_guy=${new_guy##$1*\{} | |
new_guy=${new_guy/\}/} | |
old_guy=$(declare -f $2) | |
old_guy=${old_guy/$2*{/} | |
old_guy=${old_guy/\}/} | |
eval "$2() { $new_guy $old_guy }" | |
} | |
search() { | |
echo "do the original thing" | |
} | |
sneaky() { | |
echo "wait, do this first" | |
} | |
patch sneaky search | |
search |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment