LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 01-20-2005, 06:57 PM   #1
ngwenyama
LQ Newbie
 
Registered: Jan 2005
Posts: 4

Rep: Reputation: 0
script (or other) to pass variables to C++ program


Hi all,
I would like to write a script (or something else) that does the following (in this order):
1) gets a few input variables from user (such as the name of a file)
2) starts a program that I did not write
3) when the program asks for user input (eg "what is the name of the input file?"), insert the variables obtained in step 1.
4) after step 1, I'd like everything to be done automatically. I'm doing simulations that can take a while, so would like to not have to come back to type things in when the program needs the input from setp 3 above.

Is it also possible to do this for a program that has a switch menu (eg "Type 1 to change input filename, press 2 to change output filename, press 3 to run program")?

Any help would be awesome, or just point me in the correct direction.

Thanks,
Ngwenyama
 
Old 01-20-2005, 07:16 PM   #2
leonscape
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 47
Whats the program to recieve the input?
 
Old 01-20-2005, 07:45 PM   #3
ngwenyama
LQ Newbie
 
Registered: Jan 2005
Posts: 4

Original Poster
Rep: Reputation: 0
bayesass (software to which variables are passed)

It's an abscure program called Bayesass and is for estimating migration rates between biological populations using genetic data.

http://www.rannala.org/labpages/software.html

It is the program that has switch menus to get it started. I'm working on writing a C program that will take some data and output a file that bayesass can read. However, I'll need to set other parameters in bayesass interactively (or hopefully from the script).

Ideally, I'd like to be able to input the variables once, then generate many files that bayesass can read, and then allow bayesass to process them one by one.

Hope this helps,
and thanks in advance...
Ngwenyama
 
Old 01-20-2005, 08:06 PM   #4
leonscape
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 47
Thats some switch menu!

It would be fairly easy to alter the program to take command line variables ( so you could do bayesass 34 10 .... ) from a script, It wouldn't take long ( of you'd like me to do that ) Just means compiling it your self.
 
Old 01-20-2005, 08:12 PM   #5
ngwenyama
LQ Newbie
 
Registered: Jan 2005
Posts: 4

Original Poster
Rep: Reputation: 0
by "command line variables" do you mean arguments to the command that calls the program?

Do you know whether there is any way of sequentially sending in the values when bayesass pauses for input?

Thanks
Ngwenyama
 
Old 01-20-2005, 08:31 PM   #6
leonscape
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 47
Quote:
by "command line variables" do you mean arguments to the command that calls the program?
Yes, the same way you pass variables to any other cli program so instead of entering everthing through the menu, So simply provide all the information when you call the program.

Quote:
Do you know whether there is any way of sequentially sending in the values when bayesass pauses for input?
No I don't, not from a script anyway. Since it pauses, it still has control of the shell, and isn't returning so the script can't access it. I maybe wrong about this but I've never found a way to do this with a script.

You could with a program ( C/C++ ), but then where onto a diffrent thing all together.
 
Old 01-20-2005, 08:41 PM   #7
ngwenyama
LQ Newbie
 
Registered: Jan 2005
Posts: 4

Original Poster
Rep: Reputation: 0
Leonscape, thanks so much for your help with this.

I'm interested in the C/C++ approach particularly because I'm learning C++ and because I may want to / have to do this or something like this in the future with other similar software that may not have source code available.

Can you point me in the right direction for figuring out how to do this? I can look stuff up, but don't really know where to start .

Am I correct in assuming that I need to:
1) write a c++ program that calls bayesass (how does one call another program)
2) figure out how to pass in the correct variables at the correct time
??

Thanks so much,
Ngwenyama
 
Old 01-20-2005, 08:52 PM   #8
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 66
There are two functions that you can use to execute programs from C.

Check out there man pages.
man 3 system
man 3 exec

Now there is a distinct different between these two methods. Exec replaces the current running process with a new process. As a result you typically fork before an exec.

Example:
if (fork() == 0) {
execl(....)
}

System however, executes the program and waits for it to return so your program will not get to continue executing until the other one is finished.
 
Old 01-20-2005, 09:02 PM   #9
leonscape
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 47
I've not done this for a while myself ( I use KDE to handle the messiness for me ), but you need to look up forking and piping, theres a very brief tutorial here Which is in C but you'll get the idea ( bayesass would be the child ).

BTW, Its actually easier to alter bayesass to use arguments

Last edited by leonscape; 01-20-2005 at 09:03 PM.
 
Old 01-20-2005, 09:54 PM   #10
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
I think that an 'expect' script may be able to do what you want.
 
  


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
how to pass a row to another PHP script bahadur Programming 5 12-24-2011 01:07 AM
which C++ library is used to get the options user pass for program TruongAn Programming 7 07-05-2005 04:56 PM
Pass multiple URL variables PHP agallant Programming 3 10-21-2004 07:29 PM
pass a parameter to a running program nimra Linux - Software 1 06-29-2004 01:38 AM
Passing variables from AWK script to my shell script BigLarry Programming 1 06-12-2004 04:32 AM


All times are GMT -5. The time now is 10:16 PM.

Main Menu
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