Bash from C++

Hello! I`m learning C++ and now I interested in bash (linux command interpretator) and C++. My question: how I can assign bash output to some string variable? Or what I must read that I have understood how I can do it?
P.S Sorry for my English.
Last edited on
hmm...
im not entirely sure what you're asking here.
do you mean :
1) how can i use bash in C++?
2) how can i assign some sort of cin string to work as a console of sorts with bash?
3) how can i use strings to perform bash outputs?
Answers :
1) Bash can be used, as MS-DOS can, through system("XYZ"); xyz being whatever command. system("clear"); for example.
2) Here's a simple program i made in a few minutes where it requests an input. feel free to use it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Simple Console Application For Bash

// Defining What To Include And What Namespace To Use.
#include <iostream>
#include <string> /* You Need This For Strings :) */
/* You Need One Of Those For System, Forget Which :O */
#include <stdio.h>
#include <stdlib.h>
using namespace std;

int main () {
	string cmdin;
	// LOL, ima get flame for the use of goto, i just know it.
	console:
	// the basic output line.
	cout << "C++ Bash Console > ";
	// the basic input line.
	cin >> cmdin;
	// this allows the next command input to appear on a new line
	cout << "\n";
	// all of these if statements work the same way. if the string has that value, it preforms the selected command. not much explanation to it.
	if (cmdin == "clear") {
	system("clear");
	goto console;
	} else if (cmdin == "history") {
	system("history");
	goto console;
	} else if (cmdin == "times") {
	system("times");
	goto console;
	} else if (cmdin == "time") {
	system("time");
	goto console;
	} else if (cmdin == "exit") {
	system("exit");
	} else if (cmdin == "end") {
	// this allows you to end the application. :)
	return 0;
	} else {
	// this is just incase someone passes a command that doesn't exist or is unsupported
	cout << "Invalid/Unsupported Command \n";
	goto console;
	}
}  

3) you can't to my knowledge. unless you use the method in #2. :)

EDIT : Quickly coded this, accidently used the wrong operator in a few places and forgot to declare system.
Last edited on
// LOL, ima get flame for the use of goto, i just know it.


OMFG YOU USED GOTO! Although seriously, you could just have a while loop there...

Don't use cin >> some_string;. Use getline(cin, some_string);
Anyway, you could probably just call system(cmdin.c_str()) and be done with it.
To answer the OP's question:

There are a few ways.

One is to fork() a child with the standard I/O redirected through your program. You can then capture whatever you want.

Another way is to redirect the output to a temporary file and then read the file's contents. (Don't forget to remove() the file when you are done with it.)

Good luck!

[edit] @elvenspike
The example you gave doesn't get the results of running the bash commands.
Here is an example of a simple interpreter you can build upon:
http://www.cplusplus.com/forum/windows/13523/
Last edited on
as in if it ran or failed? or what it would display if you just typed it into terminal normally? i tested it on my system, times gave output, so did history. i had no problems with anything like that.
but it wasn't supposed to be something really complicated, i haven't exactly been coding C++ for a long time. and i wasn't entirely sure what he was asking. anyway, if you look at the bottom i said i quickly coded it, plus the guy said he was a beginner as well, so i figured he wanted something basic. although, im pretty bored and have nothing to do so ill go look at your simple interpreter link. :)
Thanks for all posts. I think that Duoas understand me better than elvenspike.

Another way is to redirect the output to a temporary file and then read the file's contents. (Don't forget to remove() the file when you are done with it.)

such this?
system("ifconfig > some_file")


One is to fork() a child with the standard I/O redirected through your program. You can then capture whatever you want.

I think this is that what I need. Where I can read more about fork() and see examples with it?
I'm sorry, but I just can't let this go:
fork() a child

Evil bastard!
LOL he wants to fork a child. man is that cruel. i didn't know that parents COULD fork children. wouldn't that be child abuse? :D

anyway, yeah sorry i couldn't be more of a help bester, it wasn't really clear to me what you were asking.
also, system(cmdin.c_str()) would work for the whole console thing we were trying. fire was right. :P then add a few other things like end to end the app, etc.
Topic archived. No new replies allowed.