More about "system" call - sort of repost
-
I hope nobody gets uptight if I ask about "system" call / command. ( YES know it is NOT C /C++ question ) I realize "system" call is actually implemented by "bash script " and really have no desire to analyze it further than that. What I am looking for is to figure out how "system" outputs the "command" result to stdio ( I assume that ). I am using "system" to retrieve "hcitool" commands. I am using QTCreator and the "hcitool" commands are output to QTCreator "console". I have been successful to redirect the "system" to temporary file and then was reading the file. Works OK. I like to "redirect" the output (stdio ??) back to QT - bypassing the temp file. If that is possible? ( I tried " | tee " ... no go ) I cannot see any such option while RTFM. Here is a snippet of my current code - I am just observing the system output on console and like a "hard copy" in my main C++ application.
QElapsedTimer *ET = new QElapsedTimer();
ET->start();
QString command = "rfkill list";
// verify all
ui->textEdit->append("TRACE rfkill list");int i = system(command.toStdString().c\_str()); **how to redirect this to "textEdit" ??** qDebug() << "The value returned by system call : " << QString::number(i); QString elapsed = "Elapsed time rfkill list " + QString::number(ET->elapsed()); command = "hcitool rstat ";
.......
.
-
I hope nobody gets uptight if I ask about "system" call / command. ( YES know it is NOT C /C++ question ) I realize "system" call is actually implemented by "bash script " and really have no desire to analyze it further than that. What I am looking for is to figure out how "system" outputs the "command" result to stdio ( I assume that ). I am using "system" to retrieve "hcitool" commands. I am using QTCreator and the "hcitool" commands are output to QTCreator "console". I have been successful to redirect the "system" to temporary file and then was reading the file. Works OK. I like to "redirect" the output (stdio ??) back to QT - bypassing the temp file. If that is possible? ( I tried " | tee " ... no go ) I cannot see any such option while RTFM. Here is a snippet of my current code - I am just observing the system output on console and like a "hard copy" in my main C++ application.
QElapsedTimer *ET = new QElapsedTimer();
ET->start();
QString command = "rfkill list";
// verify all
ui->textEdit->append("TRACE rfkill list");int i = system(command.toStdString().c\_str()); **how to redirect this to "textEdit" ??** qDebug() << "The value returned by system call : " << QString::number(i); QString elapsed = "Elapsed time rfkill list " + QString::number(ET->elapsed()); command = "hcitool rstat ";
.......
.
Didn't we already cover this? [How do I pass command to system calll as variable?](https://www.codeproject.com/Answers/5321214/How-do-I-pass-command-to-system-calll-as-variable#answer1) In particular see [popen(3) - Linux manual page](https://man7.org/linux/man-pages/man3/popen.3.html) I still think that QProcess is the way to go if you're in QT land. You seem to be having difficulties getting that working - my advice would be to write as small a program as you can that uses Qprocess to do something like a directory listing, and get that working, then integrate what you learned from that to get it working in your main project. If you insist, though, you should be able to redirect output, just like you would from the terminal command line e.g.
const char *command = "rfkill list > textEdit";
system(command)
// or even just
system("rfkill list > textEdit");In this instance, I don't see the value in constructing a
QString
just to then re-format it as a C string for the system command, and then calling the QString destructor.Keep Calm and Carry On
-
Didn't we already cover this? [How do I pass command to system calll as variable?](https://www.codeproject.com/Answers/5321214/How-do-I-pass-command-to-system-calll-as-variable#answer1) In particular see [popen(3) - Linux manual page](https://man7.org/linux/man-pages/man3/popen.3.html) I still think that QProcess is the way to go if you're in QT land. You seem to be having difficulties getting that working - my advice would be to write as small a program as you can that uses Qprocess to do something like a directory listing, and get that working, then integrate what you learned from that to get it working in your main project. If you insist, though, you should be able to redirect output, just like you would from the terminal command line e.g.
const char *command = "rfkill list > textEdit";
system(command)
// or even just
system("rfkill list > textEdit");In this instance, I don't see the value in constructing a
QString
just to then re-format it as a C string for the system command, and then calling the QString destructor.Keep Calm and Carry On
Allow me to restate my objective - to learn how system "command" works and fits into my application. I will tackle QT options / objects later....final plan is to run multithreaded application with system calls or QT equivalent in background From RTFM I understand that I should be able to option the actual commend same way as when used directly in "terminal". So far only redirecting to (temporary ) file works. Single option likes " >> /tmp/temp" stops output to the console. Not desired. I should be able to add "| tee " - but obviously my syntax is wrong because it does not do... As far as redirecting to " >> textEdit " I do not see how that could work... And yes, I have a single test project to try all of this. Here is my basic test code
const char \*command = "rfkill list "; outputs so console OK qDebug() <<" system call result " << QString::number(system(command) ); command = "rfkill list >> /tmp/temp";
outputs to temporary file OK
qDebug() <<" system call result " << QString::number(system(command) );
command = "rfkill list |tee >> /tmp/temp";
no console output , may have added to test file - not checked
qDebug() <<" system call result " << QString::number(system(command) );
command = "rfkill list >> textEdit";
no output anywhere
qDebug() <<" system call result " << QString::number(system(command) );Here is my debug / application output
TEST set data
0: hci0: Bluetooth
Soft blocked: no
Hard blocked: no
2: hci1: Bluetooth
Soft blocked: no
Hard blocked: no
system call result "0"
system call result "0"
system call result "0"
system call result "0" -
Allow me to restate my objective - to learn how system "command" works and fits into my application. I will tackle QT options / objects later....final plan is to run multithreaded application with system calls or QT equivalent in background From RTFM I understand that I should be able to option the actual commend same way as when used directly in "terminal". So far only redirecting to (temporary ) file works. Single option likes " >> /tmp/temp" stops output to the console. Not desired. I should be able to add "| tee " - but obviously my syntax is wrong because it does not do... As far as redirecting to " >> textEdit " I do not see how that could work... And yes, I have a single test project to try all of this. Here is my basic test code
const char \*command = "rfkill list "; outputs so console OK qDebug() <<" system call result " << QString::number(system(command) ); command = "rfkill list >> /tmp/temp";
outputs to temporary file OK
qDebug() <<" system call result " << QString::number(system(command) );
command = "rfkill list |tee >> /tmp/temp";
no console output , may have added to test file - not checked
qDebug() <<" system call result " << QString::number(system(command) );
command = "rfkill list >> textEdit";
no output anywhere
qDebug() <<" system call result " << QString::number(system(command) );Here is my debug / application output
TEST set data
0: hci0: Bluetooth
Soft blocked: no
Hard blocked: no
2: hci1: Bluetooth
Soft blocked: no
Hard blocked: no
system call result "0"
system call result "0"
system call result "0"
system call result "0"The
tee
command splits output the man page statesSYNOPSIS
tee [OPTION]... [FILE]...For every
FILE
you add to the end of the command, tee will direct stdout to the given file. So I think you wantrfkill list | tee tempFile
This will send output to tempFile (in the current working directory) and send the output to the screen as well. You can test this from your terminal session.
Keep Calm and Carry On
-
Allow me to restate my objective - to learn how system "command" works and fits into my application. I will tackle QT options / objects later....final plan is to run multithreaded application with system calls or QT equivalent in background From RTFM I understand that I should be able to option the actual commend same way as when used directly in "terminal". So far only redirecting to (temporary ) file works. Single option likes " >> /tmp/temp" stops output to the console. Not desired. I should be able to add "| tee " - but obviously my syntax is wrong because it does not do... As far as redirecting to " >> textEdit " I do not see how that could work... And yes, I have a single test project to try all of this. Here is my basic test code
const char \*command = "rfkill list "; outputs so console OK qDebug() <<" system call result " << QString::number(system(command) ); command = "rfkill list >> /tmp/temp";
outputs to temporary file OK
qDebug() <<" system call result " << QString::number(system(command) );
command = "rfkill list |tee >> /tmp/temp";
no console output , may have added to test file - not checked
qDebug() <<" system call result " << QString::number(system(command) );
command = "rfkill list >> textEdit";
no output anywhere
qDebug() <<" system call result " << QString::number(system(command) );Here is my debug / application output
TEST set data
0: hci0: Bluetooth
Soft blocked: no
Hard blocked: no
2: hci1: Bluetooth
Soft blocked: no
Hard blocked: no
system call result "0"
system call result "0"
system call result "0"
system call result "0"To add to what k5054 says, you can test all features of the
system
command in a terminal session. All thatsystem
does is to send the string you pass it direct to the shell, as explained at system(3) - Linux manual page[^], and system, _wsystem | Microsoft Docs[^].