Communication between shell script and program
What's the best way for a shell script to interact with a binary program which is run by the script? Could this be done with environment variables? Or I was thinking, is it possible to make a virtual file - like the 'files' in /proc - and use that for communicating?
|
That depends.
Is the shell script writting input to the program or is it process output from the program or both? Could this be done with env. vars.? Maybe. Does the program know anything about the env. vars.? You need to provide more information. What's the program? Is it a common program that everyone would knwo about or is something you wrote? |
Sorry. I was in a hurry when I made this thread. I wanted to have a binary program which would be started by a shell script and would pass information back to the running shell script. The shell script may also have to pass information to the binary.
I've just started using Linux, but have been programming for Windows and DOS for a long time. This is how a simliar script may look like: Code:
./somebinary & I've just read about the 'getenv' and 'putenv' C functions. Would what I described work if I used 'putenv' (in the binary) to set $SOMEVAR to not equal zero? |
popen() lets you send/receive information (not both at the same time), but it has to start the shell script.
It would be a lot easier if you kept everything in shell. Do a search here on 'ksh coprocess' - these can communicate back and forth nicely. |
Unfortunately what I'm trying to do cannot be accomplished in a shell script and the shell script has to launch the binary, not the other way round. There isn't that much information that has to be transferred, maybe 3 numbers.
|
In shell scripts, there is a way to trap the return value.
$? I think. So if you could change the program to return a value, this might work: while [ $SOMEVAR -eq 0 ] do echo 'Processing...' SOMEVAR=$? done echo 'Complete!' Also, in "C" there is a getenv() and putenv() but would it spawn another shell and leave this shell clueless? Not sure. HTH End |
If you want to pass 3 nums to binary, then pass on the cmd line:
./somebinary 1 2 3 If you want to retrieve 3 nums from the binary when it finishes, you can trap the output like this: rtn_vals=`somebinary` That's backquotes btw. Then you have to parse $rtn_vals. Easy enough if eg space separated. $? only contains the rtn status as a single val, unless you want to do some fancy number packing.... |
All times are GMT -5. The time now is 04:28 AM. |