Results 1 to 4 of 4

Thread: Start a linux command and read its standard output using QProcess

  1. #1
    Join Date
    Mar 2009
    Location
    India
    Posts
    8
    Thanks
    2
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Start a linux command and read its standard output using QProcess

    I am trying to read the output of a linux command using QProcess but its not given the correct output.

    Qt Code:
    1. QProcess process;
    2.  
    3. process.start("iwconfig 2>&1 | grep ESSID");
    4. process.waitForFinished();
    5. usleep(1000);
    6. QString output(process.readAllStandardOutput());
    7. qDebug()<<output;
    8.  
    9. QString err(process.readAllStandardError());
    10. qDebug()<<err;
    To copy to clipboard, switch view to plain text mode 


    In this code the qDebug()<<output; prints "" and
    qDebug()<<err; prints "iwconfig: unknown command "|"
    "
    When I run this same command "iwconfig 2>&1 | grep ESSID" in terminal window its shown
    wlan0 IEEE 802.11bgn ESSID:"Arun Kumar" Nickname:"<WIFI@REALTEK>"

    Why my program printing "iwconfig: unknown command "|"", what is the error in my code and how I can correct this in my code?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,396
    Thanks
    37
    Thanked 1,542 Times in 1,493 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Start a linux command and read its standard output using QProcess

    What you are trying to do is to run two programs, one piping output into the others input by using a shell pipe redirection.

    This is a feature provided by shells, e.g. bash, dash, zsh.

    So you can either run a script that does that in a shell context, run two processes and piping output from one to the other, or simply just run the first command and filter in your program.

    Cheers,
    _

  3. The following 2 users say thank you to anda_skoa for this useful post:

    arunkumaraymuo (29th October 2015), menonakhil93 (6th November 2017)

  4. #3
    Join Date
    Mar 2009
    Location
    India
    Posts
    8
    Thanks
    2
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Start a linux command and read its standard output using QProcess

    Thanks to your reply.

    I solved the issue by updating the code to
    Qt Code:
    1. process.start("sh", QStringList()<<"-c"<<"iwconfig 2>&1 | grep ESSID");
    To copy to clipboard, switch view to plain text mode 
    Last edited by arunkumaraymuo; 29th October 2015 at 05:47. Reason: missing [code] tags

  5. #4
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Start a linux command and read its standard output using QProcess

    Write a small shell script that does exactly what you want, then execute the shell script using QProcess. For example, to take your original example, I'd write a small shell script named /path/to/test.sh as follows:

    Qt Code:
    1. #!/bin/sh
    2. iwconfig 2>&1 | grep ESSID
    3. exit $?
    To copy to clipboard, switch view to plain text mode 
    Then execute QProcess as follows:

    Qt Code:
    1. QProcess process;
    2. process.start("/path/to/test.sh");
    3. process.waitForFinished();
    4. QString output = process.readAllStandardOutput();
    5. qDebug() << output;
    6. QString err = process.readAllStandardError();
    7. qDebug() << err;
    To copy to clipboard, switch view to plain text mode 
    Of course you should add error checking, but that's the general gist of what anda_skoa is saying for one of the options he listed... You could also use two QProcess's where stdout from the 1st is piped to the 2nd QProcess's stdin, etc. Several options here, choose what's easiest for you to implement.

    Good luck.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  6. The following user says thank you to jefftee for this useful post:

    arunkumaraymuo (29th October 2015)

Similar Threads

  1. Replies: 2
    Last Post: 7th June 2013, 06:35
  2. QProcess Standard Output
    By Decessus in forum Qt Programming
    Replies: 4
    Last Post: 15th August 2012, 17:10
  3. Replies: 3
    Last Post: 23rd February 2011, 08:50
  4. Help on QProcess - Output Read
    By augusbas in forum Qt Programming
    Replies: 5
    Last Post: 24th September 2009, 11:54
  5. Replies: 2
    Last Post: 2nd June 2008, 08:45

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.