문제

I'm working on an old C software. There is one ksh script which executes a C program, which then creates some other processes and ends. These processes remain alive.

I'm trying to set an environment variable inside my ksh script, so that it could be accessible in the newly created processes that are still alive.

I have tried this way :

#!/bin/ksh

VARIABLE=value
export VARIABLE

my_c_program

But that doesn't work... I have tried to :

  1. change my ksh script to bash
  2. create a wrapper script that creates and exports the variable and then executes the original ksh script (which just executes the C program)
  3. sourcing my ksh script (or my wrapper script when trying with 2.) instead of executing it

But nothing from that worked.

The only thing that works for now is when I explicitly, by hand, execute the command :

export VARIABLE

In the current bash terminal.

Why? Isn't it possible to do the export inside a script instead of doing it manually?

도움이 되었습니까?

해결책

Everything is ok actually...

The fact is that the process I thought was the child of the C program executed in my ksh script was the child of another process executed before. The C program was just sending a message via shared memory to tell the other program to execute its child.

So indeed the environment variable never went from my C program to the other's child. The only time when I had that variable set in the child is when I executed the other program (the one which is the real parent of the child) in a shell where the variable was exported.

다른 팁

The code above looks correct and it should work. Another way to do it is:

VARIABLE=value my_c_program

which exports the variable just for the program. Afterwards, the variable will be set but other external processes don't get a copy.

So why doesn't your script work? It's hard to tell but here are some tips to debug the issue:

  1. Use #!/bin/ksh -x to enable debug output. Save the output in a file and then grep VARIABLE to make see what happens with it.

  2. Check for typos.

  3. Another shell script is like an external process. So create a script

    #!/bin/ksh
    
    echo $VARIABLE
    

    and call it instead of my_c_program just to make sure passing the variable on works.

  4. Maybe the C does something unexpected. Use a debugger to make sure it does what you expect.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top