Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. More about "system" call - sort of repost

More about "system" call - sort of repost

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++designlinuxdebugging
5 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    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 ";
    

    .......

    .

    K 1 Reply Last reply
    0
    • L Lost User

      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 ";
      

      .......

      .

      K Offline
      K Offline
      k5054
      wrote on last edited by
      #2

      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

      L 1 Reply Last reply
      0
      • K k5054

        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

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        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"

        K L 2 Replies Last reply
        0
        • L Lost User

          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"

          K Offline
          K Offline
          k5054
          wrote on last edited by
          #4

          The tee command splits output the man page states

          SYNOPSIS
          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 want

          rfkill 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

          1 Reply Last reply
          0
          • L Lost User

            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"

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            To add to what k5054 says, you can test all features of the system command in a terminal session. All that system 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[^].

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups