加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如何在bash中的另一个函数内定义一个函数

发布时间:2020-12-15 09:11:41 所属栏目:安全 来源:网络整理
导读:我有以下代码 func1(){ #some function thing function2(){ #second function thing }} 我想调用function2但我得到一个错误function2:找不到 有解决方案吗? bash中的函数定义不工作,函数定义在许多其他语言中工作。在bash中,函数定义是一个可执行命令,
我有以下代码
func1(){
    #some function thing
    function2(){
        #second function thing
    }
}

我想调用function2但我得到一个错误function2:找不到

有解决方案吗?

bash中的函数定义不工作,函数定义在许多其他语言中工作。在bash中,函数定义是一个可执行命令,用于定义函数的效果(替换任何以前的定义),与变量赋值命令定义变量的值(替换任何以前的定义)非常相似。也许这个例子会澄清我的意思:
$ outerfunc1() {
> innerfunc() { echo "Running inner function #1"; }
> echo "Running outer function #1"
> }
$ outerfunc2() {
> innerfunc() { echo "Running inner function #2"; }
> echo "Running outer function #2"
> }
$ # At this point,both outerfunc1 and outerfunc2 contain definitions of
$ # innerfunc,but since neither has been executed yet,the definitions
$ # haven't "happened".
$ innerfunc
-bash: innerfunc: command not found
$ outerfunc1
Running outer function #1
$ # Now that outerfunc1 has executed,it has defined innerfunc:
$ innerfunc
Running inner function #1
$ outerfunc2
Running outer function #2
$ # Running outerfunc2 has redefined innerfunc:
$ innerfunc
Running inner function #2

现在,如果你还不知道这一点,我很确定这不是你嵌套函数定义的原因。这引出了一个问题:为什么你嵌套函数定义?无论你期望嵌套定义有什么效果,这不是他们在bash中做的;所以1)不要他们和2)找到一些其他的方式来完成任何你正在尝试得到的嵌套为你做。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读