6 bool Shell::run(const string
&command_
, string
&output
) {
7 auto command
= prepareCommand(command_
);
8 auto *file
= popen(command
.c_str(), "r");
11 perror("Shell::run popen()");
17 auto read
= fread(data
, 1, sizeof(data
), file
);
20 output
.append(data
, read
);
23 cerr
<< "Shell::run read(): " << strerror(ferror(file
));
26 } while (!feof(file
));
28 auto error
= ferror(file
);
29 auto status
= pclose(file
);
30 return !error
&& !status
;
33 bool Shell::run(const string
&command
) {
34 auto error
= system(prepareCommand(command
).c_str());
36 perror("Shell::run system()");
42 Shell::Shell(bool redirect_stderr
)
43 : redirect
{redirect_stderr
}
45 if (!system(nullptr)) {
46 throw runtime_error("no shell available");
50 Shell::Shell(const string
&prefix_
, bool redirect_stderr
)
52 , redirect
{redirect_stderr
}
54 if (!system(nullptr)) {
55 throw runtime_error("no shell available");
59 string
Shell::prepareCommand(const string
&command_
) {
61 if (prefix
.length()) {
62 command
.append(prefix
);
65 command
.append(command_
);
67 command
.append(" 2>&1");
69 INFO("Prepared command: " << command
);