Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to execute shell command which has pipe?
QtWS25 Last Chance

How to execute shell command which has pipe?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 5 Posters 3.5k Views
  • 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.
  • MucipM Offline
    MucipM Offline
    Mucip
    wrote on last edited by
    #1

    Hi,
    I want to run below command with Qt.

    cat /proc/cpuinfo | grep Serial | cut -d ' ' -f 2

    My code is:

    QProcess process;
        process.start("cat", QStringList() <<"/proc/cpuinfo"<< "|" << "grep"<< "Serial"<< "|"<< "cut"<< "-d"<< "' '"<< "-f"<< "2", QIODevice::ReadOnly);
        process.waitForFinished(-1); // will wait forever until finished
    
        QString stdout = process.readAllStandardOutput();
        QString stderr = process.readAllStandardError();
    
        qDebug()<<stdout<< endl;
        qDebug()<<stderr<< endl;
    

    But I gues I got error with piping? Because I got below error message::

    "/bin/cat: invalid option -- 'd'\nTry '/bin/cat --help' for more information.\n"

    What the command String List must be?..

    Regards,
    Mucip:)

    aha_1980A JonBJ 2 Replies Last reply
    0
    • MucipM Mucip

      Hi,
      I want to run below command with Qt.

      cat /proc/cpuinfo | grep Serial | cut -d ' ' -f 2

      My code is:

      QProcess process;
          process.start("cat", QStringList() <<"/proc/cpuinfo"<< "|" << "grep"<< "Serial"<< "|"<< "cut"<< "-d"<< "' '"<< "-f"<< "2", QIODevice::ReadOnly);
          process.waitForFinished(-1); // will wait forever until finished
      
          QString stdout = process.readAllStandardOutput();
          QString stderr = process.readAllStandardError();
      
          qDebug()<<stdout<< endl;
          qDebug()<<stderr<< endl;
      

      But I gues I got error with piping? Because I got below error message::

      "/bin/cat: invalid option -- 'd'\nTry '/bin/cat --help' for more information.\n"

      What the command String List must be?..

      Regards,
      Mucip:)

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #5

      @Mucip
      First things first while I think of it:

      cat /proc/cpuinfo | grep Serial | cut -d ' ' -f 2
      

      That's 2 pipes. One pipe is wasted: 100% equivalent with 1 pipe is:

      grep Serial /proc/cpuinfo | cut -d ' ' -f 2
      

      [And if you know your awk or sed you could rewrite this as one command, no piping :) ]

      A separate point is whether it's worth doing any commands/piping for this. You're going to be reading the output of this back into your parent process. While you're doing that, both grep Serial and the cut are pretty simple to implement while you read the output stream in code, so as @aha_1980 has said you could get rid of the need to do any QProcess/piping if you just open /proc/cpuinfo into a QFile and do the read-parsing yourself.

      If you do want to do it via processes/piping, you have to change your code to take one of two approaches:

      1. You cannot just pass the whole command line to cat executable, that's why you are getting the error message. You have to split it up: taking my rewrite above, one QProcess for just the grep Serial /proc/cpuinfo, another one for the cut -d ' ' -f 2, with output/input piping via QProcess (no |). Efficient, but more code.

      2. You can pass the whole line to one process if you use /bin/sh or /bin/bash to handle the commands & the piping for you.

      /bin/sh -c "grep Serial /proc/cpuinfo | cut -d ' ' -f 2"
      

      Now you use just one QProcess for /bin/sh, with arg #1 being -c and arg #2 being grep Serial /proc/cpuinfo | cut -d ' ' -f 2. Marginally less efficient, but saves writing multiple QProcess.

      1 Reply Last reply
      5
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

        See QProcess::setStandardOutputProcess.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        2
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #3

          Hi
          QProcess is meant to run a single process so it will give the pipe as a parameter as you might have noticed.
          I think this is what you want.
          https://stackoverflow.com/questions/20901884/piping-or-command-chaining-with-qprocess

          1 Reply Last reply
          1
          • MucipM Mucip

            Hi,
            I want to run below command with Qt.

            cat /proc/cpuinfo | grep Serial | cut -d ' ' -f 2

            My code is:

            QProcess process;
                process.start("cat", QStringList() <<"/proc/cpuinfo"<< "|" << "grep"<< "Serial"<< "|"<< "cut"<< "-d"<< "' '"<< "-f"<< "2", QIODevice::ReadOnly);
                process.waitForFinished(-1); // will wait forever until finished
            
                QString stdout = process.readAllStandardOutput();
                QString stderr = process.readAllStandardError();
            
                qDebug()<<stdout<< endl;
                qDebug()<<stderr<< endl;
            

            But I gues I got error with piping? Because I got below error message::

            "/bin/cat: invalid option -- 'd'\nTry '/bin/cat --help' for more information.\n"

            What the command String List must be?..

            Regards,
            Mucip:)

            aha_1980A Offline
            aha_1980A Offline
            aha_1980
            Lifetime Qt Champion
            wrote on last edited by aha_1980
            #4

            @Mucip and the extraction of a serial number can easily be done in code too ;)

            Edit: thinking about it again, to read the contents of /proc/cpuinfo you don't even need cat or QProcess.

            Just open the file with QFile and find the line of interest. That's it :)

            Qt has to stay free or it will die.

            1 Reply Last reply
            3
            • MucipM Mucip

              Hi,
              I want to run below command with Qt.

              cat /proc/cpuinfo | grep Serial | cut -d ' ' -f 2

              My code is:

              QProcess process;
                  process.start("cat", QStringList() <<"/proc/cpuinfo"<< "|" << "grep"<< "Serial"<< "|"<< "cut"<< "-d"<< "' '"<< "-f"<< "2", QIODevice::ReadOnly);
                  process.waitForFinished(-1); // will wait forever until finished
              
                  QString stdout = process.readAllStandardOutput();
                  QString stderr = process.readAllStandardError();
              
                  qDebug()<<stdout<< endl;
                  qDebug()<<stderr<< endl;
              

              But I gues I got error with piping? Because I got below error message::

              "/bin/cat: invalid option -- 'd'\nTry '/bin/cat --help' for more information.\n"

              What the command String List must be?..

              Regards,
              Mucip:)

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #5

              @Mucip
              First things first while I think of it:

              cat /proc/cpuinfo | grep Serial | cut -d ' ' -f 2
              

              That's 2 pipes. One pipe is wasted: 100% equivalent with 1 pipe is:

              grep Serial /proc/cpuinfo | cut -d ' ' -f 2
              

              [And if you know your awk or sed you could rewrite this as one command, no piping :) ]

              A separate point is whether it's worth doing any commands/piping for this. You're going to be reading the output of this back into your parent process. While you're doing that, both grep Serial and the cut are pretty simple to implement while you read the output stream in code, so as @aha_1980 has said you could get rid of the need to do any QProcess/piping if you just open /proc/cpuinfo into a QFile and do the read-parsing yourself.

              If you do want to do it via processes/piping, you have to change your code to take one of two approaches:

              1. You cannot just pass the whole command line to cat executable, that's why you are getting the error message. You have to split it up: taking my rewrite above, one QProcess for just the grep Serial /proc/cpuinfo, another one for the cut -d ' ' -f 2, with output/input piping via QProcess (no |). Efficient, but more code.

              2. You can pass the whole line to one process if you use /bin/sh or /bin/bash to handle the commands & the piping for you.

              /bin/sh -c "grep Serial /proc/cpuinfo | cut -d ' ' -f 2"
              

              Now you use just one QProcess for /bin/sh, with arg #1 being -c and arg #2 being grep Serial /proc/cpuinfo | cut -d ' ' -f 2. Marginally less efficient, but saves writing multiple QProcess.

              1 Reply Last reply
              5

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved