gpt4 book ai didi

linux - 如何使用以下命名约定 : "[module]::[function name]"? 导入 Bash 函数

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:08:19 24 4
gpt4 key购买 nike


我正在一个名为 library.sh 的 .sh 文件中编写一组函数。

我希望在我的库中定义每个函数来创建一个伪 namespace ,如下所示:

library::functionA () 
{
...
}

library::functionB()
{
...
}

然后在主脚本中调用库函数如下:

. lib/library.sh
...
library::functionA

当我运行主脚本时,我收到一条错误消息:

library::functionA command not found


重新运行项目中有一个示例: https://github.com/rerun/rerun/blob/master/etc/bash_completion.sh(see list:member)
但是方法是本地调用的,只有一个冒号。

Google 的 shell 样式链接中也提到了这个想法:https://google-styleguide.googlecode.com/svn/trunk/shell.xml

under function names .. Separate libraries with ::. Parentheses are required after the function name.

我想要[模块名称]::[函数名称]。

我错过了什么?
感谢您的帮助,

最佳答案

我刚刚意识到这一点。谢谢里奇。我通过删除括号并将特定指令 typeset -fx library::test 添加到我想在 library.sh 中导出的方法来使其工作。给你们两个加一个。但是没有更好的方法从脚本中导出所有函数吗?仅供引用,排版似乎已过时。

我的问题的答案是:library.sh 之上的 typeset -f。亲爱的,这太酷了!谢谢!导出所有函数。

如果我现在在我的库中调用本地方法,我需要添加“library::”。是否有 bash 内置指令说明函数是否是本地函数,是否省略标签“library::”?

关于linux - 如何使用以下命名约定 : "[module]::[function name]"? 导入 Bash 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28902264/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com