string exec string command bool root int line system mkdir efog_temp d

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
string exec(string command, bool root, int line) {
system("mkdir efog_temp 2>/dev/null");
string command_r, command_nr;
command_r = "sudo " + command + " > efog_temp/temp";
command_nr = command + " > efog_temp/temp";
if (root) system(command_r.c_str());
else system(command_nr.c_str());
string result, temp;
int n = 0;
ifstream r;
r.open("efog_temp/temp");
while (!r.eof()) {
std::getline(r, temp);
n++;
if (line == 0 || n == line) result = result + temp + "\r";
if (line != 0 && n >= line) break;
}
system("rm -rf efog_temp");
return result;
}