Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to execute shell command which has pipe?



  • 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:)



  • @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.


  • Lifetime Qt Champion


  • Lifetime Qt Champion

    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


  • Lifetime Qt Champion

    @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 :)



  • @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.


Log in to reply