script (or other) to pass variables to C++ program
ProgrammingThis 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.
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.
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
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.
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.
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
??
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.