Skip to content

Instantly share code, notes, and snippets.

@coderofsalvation
Created February 7, 2022 17:30
Show Gist options
  • Save coderofsalvation/0cb614099c41c7025d6c2e35f4ae63a0 to your computer and use it in GitHub Desktop.
Save coderofsalvation/0cb614099c41c7025d6c2e35f4ae63a0 to your computer and use it in GitHub Desktop.
decorators / monkeypatching in bourne shell / bash
#!/bin/sh
# usage:
# ./myscript.sh foo bar
# handler called: foo bar
# i am foo
call(){ type on_$1 1>/dev/null 2>/dev/null && on_$1 "$@" || "$@"; }
on_foo(){
echo before handler called: "$@"
"$@"
echo after handler called: "$@"
}
foo(){
echo i am foo
}
call "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment