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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
01-20-2005, 06:57 PM
|
#1
|
|
LQ Newbie
Registered: Jan 2005
Posts: 4
Rep:
|
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
|
|
|
|
01-20-2005, 07:16 PM
|
#2
|
|
Senior Member
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313
Rep:
|
Whats the program to recieve the input?
|
|
|
|
01-20-2005, 07:45 PM
|
#3
|
|
LQ Newbie
Registered: Jan 2005
Posts: 4
Original Poster
Rep:
|
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
|
|
|
|
01-20-2005, 08:06 PM
|
#4
|
|
Senior Member
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313
Rep:
|
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.
|
|
|
|
01-20-2005, 08:12 PM
|
#5
|
|
LQ Newbie
Registered: Jan 2005
Posts: 4
Original Poster
Rep:
|
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
|
|
|
|
01-20-2005, 08:31 PM
|
#6
|
|
Senior Member
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313
Rep:
|
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.
|
|
|
|
01-20-2005, 08:41 PM
|
#7
|
|
LQ Newbie
Registered: Jan 2005
Posts: 4
Original Poster
Rep:
|
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
|
|
|
|
01-20-2005, 08:52 PM
|
#8
|
|
Senior Member
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Rep:
|
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.
|
|
|
|
01-20-2005, 09:02 PM
|
#9
|
|
Senior Member
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313
Rep:
|
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.
|
|
|
|
01-20-2005, 09:54 PM
|
#10
|
|
Moderator
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
I think that an 'expect' script may be able to do what you want.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 01:48 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|