【问题标题】:Environment variables not visible in shell script executed using popen in c在 c 中使用 popen 执行的 shell 脚本中不可见的环境变量
【发布时间】:2019-05-15 19:06:44
【问题描述】:

有人可以帮忙吗:

我在 c 程序中使用 popen 执行 shell 脚本。像这样的:

fd = popen("script1", "r");

script1 代码是这样的:

#!/bin/sh
source script2     #loading another script2

EXE_SOMETHING   #Function call from script2

//Do something....

script2 代码是这样的:

#!/bin/sh
function SET_ENV()
{
    oraenv <<-EOF
    SID NAME
    EOF
}

function EXE_SOMETHING()
{
    SET_ENV
    //Executing sql query using sqlplus to get output from database.
}

我的问题是我没有在 EXE_SOMETHING 中获取 SET_ENV 函数设置的环境变量,因此我找不到 sqlplus 命令。

【问题讨论】:

  • 这可能与script2EXE_SOMETHING() 中所做的事情有关。通常,一些环境变量与进程/会话相关联,EXE_SOMETHING() 中需要它们的部分(例如其他popen 调用等)将被加载到不存在这些环境变量的新环境中。 SET_ENV()EXE_SOMETHING()的内容能不能具体点?

标签: c bash shell scripting popen


【解决方案1】:

如果要使用oraenv修改当前shell环境,必须使用. oraenv调用;否则环境更改将在子shell中进行。

Oracle 文档中对此进行了解释。另请参阅 shell . 命令的文档,该命令在当前执行上下文中而不是在子 shell 中运行脚本。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-04
    • 2014-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-30
    • 1970-01-01
    相关资源
    最近更新 更多