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.
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.
// 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>
usingnamespace 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;
} elseif (cmdin == "history") {
system("history");
goto console;
} elseif (cmdin == "times") {
system("times");
goto console;
} elseif (cmdin == "time") {
system("time");
goto console;
} elseif (cmdin == "exit") {
system("exit");
} elseif (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.
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/
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?
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.