Bash 在另一个脚本中找不到导出的函数

Bash 在另一个脚本中找不到导出的函数,bash,shell,Bash,Shell,我正在尝试编写一个具有以下结构的unix脚本。基本上,我希望有一个我一直使用的函数的“库”脚本,并使用export-f functionName导出所有函数。使用库函数的实际脚本将首先运行库脚本以执行导出命令,然后理论上可以访问刚刚导出的函数。但是,这并不像错误显示的那样起作用。这两个脚本都已chmod 777”过测试。我正在尝试下面这些例子。这些都不是某些产品代码的替代品。我复制并粘贴了我正在尝试的内容 LibraryFunctions.sh: #!/bin/bash function ge

我正在尝试编写一个具有以下结构的unix脚本。基本上,我希望有一个我一直使用的函数的“库”脚本,并使用
export-f functionName
导出所有函数。使用库函数的实际脚本将首先运行库脚本以执行导出命令,然后理论上可以访问刚刚导出的函数。但是,这并不像错误显示的那样起作用。这两个脚本都已
chmod 777
”过测试。我正在尝试下面这些例子。这些都不是某些产品代码的替代品。我复制并粘贴了我正在尝试的内容

LibraryFunctions.sh:

#!/bin/bash

function getHelloWorldString() {
    echo "Hello World"
} 
export -f getHelloWorldString
#!/bin/bash

function getHelloWorldString() {
    echo "Hello World"
} 
TestLibraryFunctions.sh

#!/bin/bash

./LibraryFunctions.sh

function testExportedHelloWorld () {
    echo $(getHelloWorldString)
}
testExportedHelloWorld
#!/bin/bash

source LibraryFunctions.sh

function testExportedHelloWorld () {
    echo $(getHelloWorldString)
}
testExportedHelloWorld
结果:

me@myHost:~/testDir $ ./TestLibraryFunctions.sh
./TestLibraryFunctions.sh: line 6: getHelloWorldString: command not found
编辑:适合我的解决方案:

已从LibraryFunctions.sh中删除导出-f getHelloWorldString

#!/bin/bash

./LibraryFunctions.sh

function testExportedHelloWorld () {
    echo $(getHelloWorldString)
}
testExportedHelloWorld
#!/bin/bash

source LibraryFunctions.sh

function testExportedHelloWorld () {
    echo $(getHelloWorldString)
}
testExportedHelloWorld
从TestLibraryFunctions.sh中删除了
/LibraryFunctions.sh

#!/bin/bash

./LibraryFunctions.sh

function testExportedHelloWorld () {
    echo $(getHelloWorldString)
}
testExportedHelloWorld
#!/bin/bash

source LibraryFunctions.sh

function testExportedHelloWorld () {
    echo $(getHelloWorldString)
}
testExportedHelloWorld
从顶部的TestLibraryFunctions.sh添加了
源库函数.sh

-------新文件------:

LibraryFunctions.sh:

#!/bin/bash

function getHelloWorldString() {
    echo "Hello World"
} 
export -f getHelloWorldString
#!/bin/bash

function getHelloWorldString() {
    echo "Hello World"
} 
TestLibraryFunctions.sh

#!/bin/bash

./LibraryFunctions.sh

function testExportedHelloWorld () {
    echo $(getHelloWorldString)
}
testExportedHelloWorld
#!/bin/bash

source LibraryFunctions.sh

function testExportedHelloWorld () {
    echo $(getHelloWorldString)
}
testExportedHelloWorld

谢谢大家

导出变量或函数仅使其在执行
导出的shell的子进程中可用。执行命令(包括运行shell脚本)时,该脚本将在子进程中运行。因此,您正在原始
TestLibraryFunctions.sh
进程的子进程中运行
LibraryFunctions.sh
。回到原始脚本时,您不是
LibraryFunctions.sh
的子脚本,因此导出的函数不可见

如果要在与当前进程相同的进程中运行shell脚本,请使用
source
命令执行它

source LibraryFunctions.sh

请注意,如果执行此操作,则不需要导出函数,因为定义发生在同一个shell进程中,并且没有需要使用它的子shell进程。

导出变量或函数只会使它在执行导出的shell的子进程中可用。执行命令(包括运行shell脚本)时,该脚本将在子进程中运行。因此,您正在原始
TestLibraryFunctions.sh
进程的子进程中运行
LibraryFunctions.sh
。回到原始脚本时,您不是
LibraryFunctions.sh
的子脚本,因此导出的函数不可见

如果要在与当前进程相同的进程中运行shell脚本,请使用
source
命令执行它

source LibraryFunctions.sh

请注意,如果执行此操作,则不需要导出函数,因为定义发生在同一个shell进程中,并且没有子shell进程需要使用它。

更改为
/LibraryFunctions.sh
和其他功能应该开始工作。在这种情况下不需要导出
。它使函数在子shell中可用,这是您在这里不使用的。正如Harald指出的,您正在以脚本的形式运行
LibraryFunctions.sh
,因此它在自己的shell会话中运行。您需要将其源代码(
)导入当前shell会话,以使其
导出的函数和变量可见。不要编辑您的问题以包含答案-将答案作为适当的单独答案发布(如果它实质上增加了现有答案)。更改为
/LibraryFunctions.sh
和其他功能应该开始工作。在这种情况下不需要导出
。它使函数在子shell中可用,这是您在这里不使用的。正如Harald指出的,您正在以脚本的形式运行
LibraryFunctions.sh
,因此它在自己的shell会话中运行。您需要将其源代码(
)导入当前shell会话,以使其
导出的函数和变量可见。不要编辑问题以包含答案-将答案作为适当的单独答案发布(如果它实质上增加了现有答案).我刚刚意识到这是一个我希望的更好的解决方案,因为现在我不必管理导出所有这些功能。我刚刚意识到这是一个我希望的更好的解决方案,因为现在我不必管理导出所有这些功能。