Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved How to execute shell command which has pipe?

    General and Desktop
    5
    5
    2169
    Loading More Posts
    • 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.
    • Mucip
      Mucip last edited by

      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_1980 JonB 2 Replies Last reply Reply Quote 0
      • JonB
        JonB @Mucip last edited by JonB

        @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 Reply Quote 5
        • SGaist
          SGaist Lifetime Qt Champion last edited by

          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 Reply Quote 2
          • mrjj
            mrjj Lifetime Qt Champion last edited by

            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 Reply Quote 1
            • aha_1980
              aha_1980 Lifetime Qt Champion @Mucip last edited by aha_1980

              @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 Reply Quote 3
              • JonB
                JonB @Mucip last edited by JonB

                @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 Reply Quote 5
                • First post
                  Last post