LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 08-13-2004, 02:36 PM   #1
ZooL
Member
 
Registered: Aug 2004
Location: England
Distribution: Slackware 10.1
Posts: 41

Rep: Reputation: 15
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?
 
Old 08-13-2004, 03:41 PM   #2
SheldonPlankton
Member
 
Registered: Jun 2004
Posts: 129

Rep: Reputation: 15
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?
 
Old 08-13-2004, 05:45 PM   #3
ZooL
Member
 
Registered: Aug 2004
Location: England
Distribution: Slackware 10.1
Posts: 41

Original Poster
Rep: Reputation: 15
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?

Last edited by ZooL; 08-13-2004 at 05:59 PM.
 
Old 08-13-2004, 05:58 PM   #4
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
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.
 
Old 08-13-2004, 06:02 PM   #5
ZooL
Member
 
Registered: Aug 2004
Location: England
Distribution: Slackware 10.1
Posts: 41

Original Poster
Rep: Reputation: 15
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.
 
Old 08-13-2004, 08:48 PM   #6
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 659

Rep: Reputation: 160Reputation: 160
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
 
Old 08-14-2004, 06:44 AM   #7
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
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....
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script to check ftp communication yuva_mca Linux - General 2 12-01-2005 08:15 AM
how to cll shell script from c program mili Programming 3 04-27-2005 12:22 PM
Starting a program from a shell script Suinatsa Programming 2 04-17-2005 01:16 AM
running a program from shell script Suinatsa Programming 10 04-14-2005 12:25 PM
call a c program in a shell script jagman Programming 4 04-05-2005 05:58 PM


All times are GMT -5. The time now is 05:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration