确定是否在bash存在功能 [英] Determine if a function exists in bash
问题描述
目前我做的这是从庆典执行一些单元测试。单元测试被初始化,执行和bash脚本清理。这个脚本usualy包含的init(),执行()和清理()函数。但它们并不是强制性的。我想测试,如果他们还是没有定义。
我pviously这样做$ P $由greping和seding源,但它似乎是错误的。有没有更优雅的方式来做到这一点?
编辑:sniplet就像一个魅力以下内容:
fn_exists()
{
输入$ 1 | grep的-qshell函数
}
我认为你正在寻找的类型命令。它会告诉你是否是一个函数,内置函数,外部命令,或者只是没有定义。例如:
$ Foo类型
庆典:类型:富:未找到$输入ls
LS化名为'LS --color =自动'$哪种类型$ type类型
类型是一个shell内建$类型-t RVM
功能如果$ [-n$(输入-t RVM)]&放大器;&安培; [$(输入-t RVM)=功能]然后回声RVM是一个函数;否则回声RVM不是一个函数;科幻
RVM是一个功能
Currently I'm doing some unit tests which are executed from bash. Unit tests are initialized, executed and cleaned up in a bash script. This script usualy contains an init(), execute() and cleanup() functions. But they are not mandatory. I'd like to test if they are or are not defined.
I did this previously by greping and seding the source, but it seemed wrong. Is there a more elegant way to do this?
Edit: The following sniplet works like a charm:
fn_exists()
{
type $1 | grep -q 'shell function'
}
I think you're looking for the 'type' command. It'll tell you whether something is a function, built-in function, external command, or just not defined. Example:
$ type foo
bash: type: foo: not found
$ type ls
ls is aliased to `ls --color=auto'
$ which type
$ type type
type is a shell builtin
$ type -t rvm
function
$ if [ -n "$(type -t rvm)" ] && [ "$(type -t rvm)" = function ]; then echo rvm is a function; else echo rvm is NOT a function; fi
rvm is a function
这篇关于确定是否在bash存在功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!