bash 检查参数是否定义_如何检查功能是否已在Bash中定义?

bash 检查参数是否定义

I would like to know whether a function, say f1(), has already been defined in Bash. How should I do it?

我想知道是否已经在Bash中定义了一个函数f1() 。 我该怎么办?

You can use the type -t to get a string of the type of the name:

您可以使用type -t来获取名称类型的字符串

If the -t option is used, type prints a string which is one of alias, keyword, function, builtin, or file if name is an alias, shell reserved word, function, builtin, or disk file, respectively.

如果使用-t选项,则类型将打印一个字符串,该字符串是别名,关键字,函数,内置文件或文件之一,如果名称是别名, shell保留字,函数,内置文件或磁盘文件。

So this piece of code check whether a function has already been defined:

因此,这段代码检查是否已定义函数:

tf=$(type -t f1 2>/dev/null || rt=$?)
if [[ "$tf" != "function" ]]; then
    echo "f1 is defined as a function"
fi

The rt=$? here makes sure that statement’s return code is 0 so that it works well with set -o errexit.

rt=$? 这里确保该语句的返回码为0,以便它与set -o errexit一起很好地工作。

Answered by Eric Z Ma.
埃里克·马(Eric Z Ma)回答。

翻译自: https://www.systutorials.com/how-to-check-whether-a-function-has-been-defined-in-bash/

bash 检查参数是否定义

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值