LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Communication between shell script and program (https://www.linuxquestions.org/questions/programming-9/communication-between-shell-script-and-program-217215/)

ZooL 08-13-2004 01:36 PM

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?

SheldonPlankton 08-13-2004 02:41 PM

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?

ZooL 08-13-2004 04:45 PM

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 &
while [ $SOMEVAR -eq 0 ]
do
 echo 'Processing...'
done
echo 'Complete!'

The binary would set the value of $SOMEVAR to something other than 0 when it had finished, the shell script would then break out of the loop and display 'Complete!' on the terminal.

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?

jim mcnamara 08-13-2004 04:58 PM

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.

ZooL 08-13-2004 05:02 PM

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.

AnanthaP 08-13-2004 07:48 PM

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

chrism01 08-14-2004 05:44 AM

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.