Search code examples
cbashshellscriptingpopen

Environment variables not visible in shell script executed using popen in c


Can somebody help on this:

I'm executing shell script using popen in c program. Something like this:

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

script1 code is like this:

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

EXE_SOMETHING   #Function call from script2

//Do something....

script2 code is like this:

#!/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.
}

My problem is i'm not getting env variables in EXE_SOMETHING which are being set by SET_ENV function, due to which I'm getting sqlplus command not found.


Solution

  • If you want to use oraenv to modify the current shell environment, you must invoke it with . oraenv; otherwise the environment changes will be made in a subshell.

    This is explained in the Oracle docs. Also see the documentation for the shell . command, which runs a script inside the current execution context rather than in a subshell.