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. QProcess Class cannot get the output content when executing shell script?
Servers for Qt installer are currently down

QProcess Class cannot get the output content when executing shell script?

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 1.1k Views 2 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.
  • J Offline
    J Offline
    jiaming
    wrote on last edited by jiaming
    #1

    proc2.start("./a.sh");
    proc2.waitForFinished();
    qDebug() << proc2.readAll();

    The a.sh file is placed in the build directory. The command ./a.sh can run normally under the build directory when I use terminal.
    The content of the a.sh file is "ps -ef|grep xterm"

    At the beginning, I used the following code to execute the shell command directly. When I found that there was no output, I used the above method to run a shell script. How can I do that

    proc2.start("ps -ef|grep xterm") ;
    proc2.waitForFinished();
    qDebug() << proc2.readAll();

    eyllanescE 1 Reply Last reply
    0
    • J jiaming

      proc2.start("./a.sh");
      proc2.waitForFinished();
      qDebug() << proc2.readAll();

      The a.sh file is placed in the build directory. The command ./a.sh can run normally under the build directory when I use terminal.
      The content of the a.sh file is "ps -ef|grep xterm"

      At the beginning, I used the following code to execute the shell command directly. When I found that there was no output, I used the above method to run a shell script. How can I do that

      proc2.start("ps -ef|grep xterm") ;
      proc2.waitForFinished();
      qDebug() << proc2.readAll();

      eyllanescE Offline
      eyllanescE Offline
      eyllanesc
      wrote on last edited by eyllanesc
      #2

      @jiaming use /bin/sh /fullpath/of/a.sh. See https://stackoverflow.com/questions/9086771/how-to-start-a-shell-script-with-qprocess

      If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

      J 1 Reply Last reply
      0
      • eyllanescE eyllanesc

        @jiaming use /bin/sh /fullpath/of/a.sh. See https://stackoverflow.com/questions/9086771/how-to-start-a-shell-script-with-qprocess

        J Offline
        J Offline
        jiaming
        wrote on last edited by
        #3

        @eyllanesc said in QProcess Class cannot get the output content when executing shell script?:

        /bin/sh

        It works. Thank you.

        May I ask why this code doesn't work?

        proc2.start("ps -ef|grep xterm") ;
        proc2.waitForFinished();
        qDebug() << proc2.readAll();

        eyllanescE 1 Reply Last reply
        0
        • Kent-DorfmanK Offline
          Kent-DorfmanK Offline
          Kent-Dorfman
          wrote on last edited by
          #4

          @jiaming said in QProcess Class cannot get the output content when executing shell script?:

          May I ask why this code doesn't work

          two separate issues:
          first insance of calling a shell script --
          It has to do with the child process environment. the PATH is only aware of the system default when the child shell starts.

          second instance of calling a piped command --
          pipes are a shell function and you call ps directly so the child process isn't running under a shell that can interpret the pipe.

          1 Reply Last reply
          0
          • J jiaming

            @eyllanesc said in QProcess Class cannot get the output content when executing shell script?:

            /bin/sh

            It works. Thank you.

            May I ask why this code doesn't work?

            proc2.start("ps -ef|grep xterm") ;
            proc2.waitForFinished();
            qDebug() << proc2.readAll();

            eyllanescE Offline
            eyllanescE Offline
            eyllanesc
            wrote on last edited by
            #5

            @jiaming try with "/bin/sh -c 'ps -ef|grep xterm'"

            If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

            J 1 Reply Last reply
            0
            • eyllanescE eyllanesc

              @jiaming try with "/bin/sh -c 'ps -ef|grep xterm'"

              J Offline
              J Offline
              jiaming
              wrote on last edited by
              #6

              @eyllanesc

              proc2.start("/bin/sh -c \"ps -ef|grep xterm\"");
              

              Thank you.

              JonBJ 1 Reply Last reply
              0
              • J jiaming

                @eyllanesc

                proc2.start("/bin/sh -c \"ps -ef|grep xterm\"");
                

                Thank you.

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

                @jiaming
                You do not say which version of Qt you are using. The solution you have picked uses overload QProcess::start(const QString &commandline). That overload has been removed in recent versions of Qt 5, e.g. it is not present in latest Qt 5.15, it will not work going forward.

                I recommend you change over to the supported void QProcess::start(const QString &program, const QStringList &arguments, QIODevice::OpenMode mode = ReadWrite).

                proc2.start("/bin/sh", QStringList() <<  "-c" << "ps -ef | grep xterm");
                

                Having said that. You are going to be reading the output from this command from stdout (else there would be no point in executing it). By the time you read the lines coming in you could simply look in them for the desired xterm string via your own code, saving the need to use /bin/sh, a pipe and running grep, i.e. just

                proc2.start("ps", QStringList() << "-ef");
                

                Up to you.

                P.S.
                If you really don't want to look through the output for xterm in your own code, then if your ps takes a -C argument (e.g. Ubuntu does) you should find that the following command, without /bin/sh, pipe or grep, does the job (and does not pick up presumably-unintended matches on the string xterm):

                proc2.start("ps", QStringList() << "-C" << "xterm");
                
                J 1 Reply Last reply
                1
                • JonBJ JonB

                  @jiaming
                  You do not say which version of Qt you are using. The solution you have picked uses overload QProcess::start(const QString &commandline). That overload has been removed in recent versions of Qt 5, e.g. it is not present in latest Qt 5.15, it will not work going forward.

                  I recommend you change over to the supported void QProcess::start(const QString &program, const QStringList &arguments, QIODevice::OpenMode mode = ReadWrite).

                  proc2.start("/bin/sh", QStringList() <<  "-c" << "ps -ef | grep xterm");
                  

                  Having said that. You are going to be reading the output from this command from stdout (else there would be no point in executing it). By the time you read the lines coming in you could simply look in them for the desired xterm string via your own code, saving the need to use /bin/sh, a pipe and running grep, i.e. just

                  proc2.start("ps", QStringList() << "-ef");
                  

                  Up to you.

                  P.S.
                  If you really don't want to look through the output for xterm in your own code, then if your ps takes a -C argument (e.g. Ubuntu does) you should find that the following command, without /bin/sh, pipe or grep, does the job (and does not pick up presumably-unintended matches on the string xterm):

                  proc2.start("ps", QStringList() << "-C" << "xterm");
                  
                  J Offline
                  J Offline
                  jiaming
                  wrote on last edited by
                  #8

                  @JonB
                  The QT version I use is 5.12.
                  I tried the "ps -C xterm" command, which is not what I want. I need a detailed startup command, just like using -ef output. Is there any better suggestion?

                  JonBJ 1 Reply Last reply
                  0
                  • J jiaming

                    @JonB
                    The QT version I use is 5.12.
                    I tried the "ps -C xterm" command, which is not what I want. I need a detailed startup command, just like using -ef output. Is there any better suggestion?

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

                    @jiaming said in QProcess Class cannot get the output content when executing shell script?:

                    Is there any better suggestion?

                    Of course! I only put that in as a "P.S." if it gave what you wanted. I already showed you two other examples (both using -ef) of what to use if not ps -C.... And told you what you presently have may work in Qt 5.12 but will not if/when you move to a later Qt version.

                    J 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @jiaming said in QProcess Class cannot get the output content when executing shell script?:

                      Is there any better suggestion?

                      Of course! I only put that in as a "P.S." if it gave what you wanted. I already showed you two other examples (both using -ef) of what to use if not ps -C.... And told you what you presently have may work in Qt 5.12 but will not if/when you move to a later Qt version.

                      J Offline
                      J Offline
                      jiaming
                      wrote on last edited by
                      #10

                      @JonB

                      proc2.start("ps", QStringList() << "e" << "-C" << "xterm");
                      

                      That's what I want. Thank you

                      1 Reply Last reply
                      1

                      • Login

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