在另一个脚本中找不到导出的函数 [英] Exported function not found in another script

查看:39
本文介绍了在另一个脚本中找不到导出的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写具有以下结构的unix脚本.基本上,我想拥有一个我一直使用的函数的库"脚本,并使用 export -f functionName 导出所有函数.使用库函数的实际脚本将首先运行库脚本以执行导出命令,然后在理论上可以访问刚刚导出的函数.但是,这不能正常工作,如错误所示.这两个脚本都经过了 chmod 777 的测试.我确实在下面尝试这些示例.这些不能替代某些地方的产品代码.我复制并粘贴了我要尝试的内容.

LibraryFunctions.sh:

 #!/bin/bash函数getHelloWorldString(){回声你好世界"}导出-f getHelloWorldString 

TestLibraryFunctions.sh

 #!/bin/bash./LibraryFunctions.sh函数testExportedHelloWorld(){回声$ {getHelloWorldString)}testExportedHelloWorld 

结果:

  me @ myHost:〜/testDir $ ./TestLibraryFunctions.sh./TestLibraryFunctions.sh:第6行:getHelloWorldString:找不到命令 

适用于我的解决方案:

从LibraryFunctions.sh中删除了 export -f getHelloWorldString

从TestLibraryFunctions.sh中删除了 ./LibraryFunctions.sh

在顶部的TestLibraryFunctions.sh中添加了 source LibraryFunctions.sh

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

LibraryFunctions.sh:

 #!/bin/bash函数getHelloWorldString(){回声你好世界"} 

TestLibraryFunctions.sh

 #!/bin/bash源LibraryFunctions.sh函数testExportedHelloWorld(){回声$ {getHelloWorldString)}testExportedHelloWorld 

谢谢!

解决方案

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

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

 源LibraryFunctions.sh 

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

I am trying to write a unix script that has the structure below. Basically, I want to have a "Library" script of functions that I use all the time, and export all the functions using export -f functionName. The actual script using the library functions would first run the library script to perform the export commands and would then theoretically have access to the functions just exported. However, this does not work as shown by the error. Both scripts have been chmod 777'ed for testing. I am literally trying these examples below. These are not substitutes for some prod code somewhere. I copied and pasted what I am trying.

LibraryFunctions.sh:

#!/bin/bash

function getHelloWorldString() {
    echo "Hello World"
} 
export -f getHelloWorldString

TestLibraryFunctions.sh

#!/bin/bash

./LibraryFunctions.sh

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

Result:

me@myHost:~/testDir $ ./TestLibraryFunctions.sh
./TestLibraryFunctions.sh: line 6: getHelloWorldString: command not found

Edit: Solution that worked for me:

Removed export -f getHelloWorldString from LibraryFunctions.sh

Removed ./LibraryFunctions.shfrom TestLibraryFunctions.sh

Added source LibraryFunctions.shfrom TestLibraryFunctions.sh at the top

------- New files ------:

LibraryFunctions.sh:

#!/bin/bash

function getHelloWorldString() {
    echo "Hello World"
} 

TestLibraryFunctions.sh

#!/bin/bash

source LibraryFunctions.sh

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

Thanks all!

解决方案

Exporting a variable or function only makes it available in child processes of the shell that performed the export. When you execute a command, including running a shell script, that script runs in a child process. So you're running LibraryFunctions.sh in a child of the original TestLibraryFunctions.sh process. When you get back to the original script, you're not in a child of LibraryFunctions.sh, so exported functions are not visible.

If you want to run a shell script in the same process as the current process, use the source or . command to execute it.

source LibraryFunctions.sh

Note that if you do this, you don't need to export the function, since the definition happens in the same shell process, and there are no child shell processes that need to use it.

这篇关于在另一个脚本中找不到导出的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆