Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fn_exists()
- {
- type $1 | grep -q 'shell function'
- }
- batzel@batzel-laptop:~$ type foo
- bash: type: foo: not found
- batzel@batzel-laptop:~$ type ls
- ls is aliased to `ls --color=auto'
- batzel@batzel-laptop:~$ which type
- batzel@batzel-laptop:~$ type type
- type is a shell builtin
- $ g() { return; }
- $ declare -f g > /dev/null; echo $?
- 0
- $ declare -f j > /dev/null; echo $?
- 1
- test_declare () {
- a () { echo 'a' ;}
- declare -f a > /dev/null
- }
- test_type () {
- a () { echo 'a' ;}
- type a | grep -q 'is a function'
- }
- echo 'declare'
- time for i in $(seq 1 1000); do test_declare; done
- echo 'type'
- time for i in $(seq 1 100); do test_type; done
- real 0m0.064s
- user 0m0.040s
- sys 0m0.020s
- type
- real 0m2.769s
- user 0m1.620s
- sys 0m1.130s
- fn_exists()
- {
- type $1 2>/dev/null | grep -q 'is a function'
- }
- fn_exists test_function
- if [ $? -eq 0 ]; then
- echo 'Function exists!'
- else
- echo 'Function does not exist...'
- fi
- test_function () {
- ! type -f $1 >/dev/null 2>&1 && type -t $1 >/dev/null 2>&1
- }
Add Comment
Please, Sign In to add comment