question  How do I run a Shell Script/shell commands within C++?

lazyrussian (7)   Link to this post
Hi,

I'm somewhat of a beginner at C++ (only been around OOP for 6 months or so now - started with Java).

Anyway, with that being said, I hope someone can write a simplified answer to my problem or point me to a page with an answer for this.

Problem: I need to have C++ script execute a set of shell commands (tcsh and/or bash environment) on a machine running Ubuntu or Fedora Core.

The user will launch a graphical interface (written in C++) from the shell, and within there, he/she will utilize an option that performs a task within the shell, using shell commands.

So I need to program this new option in C++ (to incorporate it into the C++ GUI system), but the commands I want to run need to be written in shell script.

Thanks in advance for any and all help!
bnbertha (401)   Link to this post
To start a new process off you would usually use a fork() followed by one of the exec() family of calls.

I should point out that most shell commands have a 'C' code API equivalent. There are very few occasions when something cannot be done in C/C++.
Duoas (2964)   Link to this post
Much as I hate language war threads... If you are just starting you might want to check out Tcl/Tk. It is designed specifically to do the kinds of things you want to do. http://wiki.tcl.tk/ Chances are it is already available on your system and your customers' systems.

If you are already bound to C++ then ignore me. :-)
Last edited on
lazyrussian (7)   Link to this post
Thank you both for the reply. I ended up getting most of my goals achieved witht he System() command function :)

Now, I have a new problem that arose.

So, via the shell (and within my C++ script) I need to launch ROOT (C++ Framework for Physicists) and run a few commands in ROOT. I can launch root just fine with system(root), but how do I run root-specific commands through my C++ program - is it possible?

Thanks!
Duoas (2964)   Link to this post
Google "popen". Good luck!
lazyrussian (7)   Link to this post
Thanks for all your help!
lipune (46)   Link to this post
How we write code of ipc ,socket and thread with c++ as all the ipc mechanisim are written with c language code,how we mix up c++ with ipc,socket,thread.

This topic is archived - New replies not allowed.