I am writing on linux. Running bash commands like this:

system("команда"); 

Everything is good, but how do I get an answer? That is, the result of the command that is displayed in the console.

1 answer 1

If you write with Qt, then it makes sense to use its functionality - QProcess

 const int NO_TIMEOUT = -1; QProcess proc; proc.start("/bin/bash", QStringList() << "echo \"Hello World\""); proc.waitForFinished(NO_TIMEOUT); QString proc_stdout = proc.readAllStandardOutput(); QString proc_stderr = proc.readAllStandardError(); 

Another example

  • No team works. I make the following command: mkdir / root / backup; tar -cvzf /root/backup/22.33.16-26.7.16.tar.gz / root / db / The answer in the proc_stderr variable is: / bin / bash: mkdir / root / backup; tar -cvzf /root/backup/22.33.16-26.7.16.tar.gz / root / db /: ??? ?????? ????? ??? ???????? - Nikola Krivosheya
  • Is the command executed in the terminal? - free_ze pm
  • yes, it is fulfilled - Nikola Krivosheya
  • Unlike the system, QProcess starts the process with arguments, and does not execute the line (as a command line). Those. programs can be run only one at a time. proc.start ("mkdir", QStringList () << "/ root / backup"); ... proc.start ("tar", QStringList () << "- cvzf" << "/ root / backup / 22.33.16-26.7.16.tar.gz" << "/ root / db /") ; ... - Chorkov
  • @Chorkov for such a case bash you need to pass the parameter "-c" and everything will be fine. - free_ze