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 run bash script
Forum Updated to NodeBB v4.3 + New Features

How run bash script

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 2 Posters 875 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.
  • M Offline
    M Offline
    Mihaill
    wrote on last edited by
    #1

    Hi!
    I have script :

    sudo -S <<< 1qaz ./live555ProxyServer -v rtsp://stream.com:554/Rz.32.02/
    

    How I can run this script? How I can run this script in QProcess?

    JonBJ 1 Reply Last reply
    0
    • M Mihaill

      Hi!
      I have script :

      sudo -S <<< 1qaz ./live555ProxyServer -v rtsp://stream.com:554/Rz.32.02/
      

      How I can run this script? How I can run this script in QProcess?

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

      @Mihaill
      If you want to stick with using <<< you would have to run your command via bash -c "sudo -S <<< 1qaz ./live555ProxyServer -v rtsp://stream.com:554/Rz.32.02/".

      If you don't want to invoke bash -c you would have to invoke sudo -S and then send the string 1qaz ./live555ProxyServer -v rtsp://stream.com:554/Rz.32.02/ via QProcess::write() so that it arrives at the sub-process's stdin.

      To make it work as-is

      QProcess p;
      p.start("/bin/bash", { "-c", "sudo -S <<< 1qaz ./live555ProxyServer -v rtsp://stream.com:554/Rz.32.02/" } );
      

      Untested, but the second way might work with

      p.start("sudo", { "-S" });
      p.write("1qaz ./live555ProxyServer -v rtsp://stream.com:554/Rz.32.02/");  # string might need a terminating `\n`, I don't know
      

      On a separate note, for my own interest. This whole use of sudo with a Here-document (the <<<) looks "odd" to me. In principle

      command <<< some text
      

      ought to be the same as

      echo "some text" | command
      

      But, for unknown/undocumented reason it is not for the sudo. sudo -S "password echo hello" works, but echo "password echo hello" | sudo -S gives a sudo "usage" error message. That implies to me that sudo behaves differently depending on whether stdin is or is not attached to a terminal....

      UPDATE
      I misunderstood how <<< works. Turns out, it only reads the next token, not the rest of the line, to present as stdin. So

      command <<< some text
      

      actually equates to echo some | command text. One would need command <<< "some text" to equate to echo "some text" | command.

      As a result of this, my second "untested" proposal above would need to be changed to:

      p.start("sudo", { "-S", "./live555ProxyServer", "-v", "rtsp://stream.com:554/Rz.32.02/" });
      p.write("1qaz");  # string might need a terminating `\n`, I don't know
      
      M 1 Reply Last reply
      0
      • JonBJ JonB

        @Mihaill
        If you want to stick with using <<< you would have to run your command via bash -c "sudo -S <<< 1qaz ./live555ProxyServer -v rtsp://stream.com:554/Rz.32.02/".

        If you don't want to invoke bash -c you would have to invoke sudo -S and then send the string 1qaz ./live555ProxyServer -v rtsp://stream.com:554/Rz.32.02/ via QProcess::write() so that it arrives at the sub-process's stdin.

        To make it work as-is

        QProcess p;
        p.start("/bin/bash", { "-c", "sudo -S <<< 1qaz ./live555ProxyServer -v rtsp://stream.com:554/Rz.32.02/" } );
        

        Untested, but the second way might work with

        p.start("sudo", { "-S" });
        p.write("1qaz ./live555ProxyServer -v rtsp://stream.com:554/Rz.32.02/");  # string might need a terminating `\n`, I don't know
        

        On a separate note, for my own interest. This whole use of sudo with a Here-document (the <<<) looks "odd" to me. In principle

        command <<< some text
        

        ought to be the same as

        echo "some text" | command
        

        But, for unknown/undocumented reason it is not for the sudo. sudo -S "password echo hello" works, but echo "password echo hello" | sudo -S gives a sudo "usage" error message. That implies to me that sudo behaves differently depending on whether stdin is or is not attached to a terminal....

        UPDATE
        I misunderstood how <<< works. Turns out, it only reads the next token, not the rest of the line, to present as stdin. So

        command <<< some text
        

        actually equates to echo some | command text. One would need command <<< "some text" to equate to echo "some text" | command.

        As a result of this, my second "untested" proposal above would need to be changed to:

        p.start("sudo", { "-S", "./live555ProxyServer", "-v", "rtsp://stream.com:554/Rz.32.02/" });
        p.write("1qaz");  # string might need a terminating `\n`, I don't know
        
        M Offline
        M Offline
        Mihaill
        wrote on last edited by Mihaill
        #3

        @JonB said in How run bash script:

        p.start("/bin/bash", { "-c", "sudo -S <<< 1qaz ./live555ProxyServer -v rtsp://stream.com:554/Rz.32.02/" } );
        p.start("sudo", { "-S" });
        p.write("1qaz ./live555ProxyServer -v rtsp://stream.com:554/Rz.32.02/"); #

        I try use this, but when I try get output from console -> I get ""

        p.readAll()  == "";
        

        But errorString() == "Unknown error";
        How I can get output from console?

        JonBJ 1 Reply Last reply
        0
        • M Mihaill

          @JonB said in How run bash script:

          p.start("/bin/bash", { "-c", "sudo -S <<< 1qaz ./live555ProxyServer -v rtsp://stream.com:554/Rz.32.02/" } );
          p.start("sudo", { "-S" });
          p.write("1qaz ./live555ProxyServer -v rtsp://stream.com:554/Rz.32.02/"); #

          I try use this, but when I try get output from console -> I get ""

          p.readAll()  == "";
          

          But errorString() == "Unknown error";
          How I can get output from console?

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

          @Mihaill
          Now you need to look up how QProcess works. Your readAll() call is too early, the QProcess::start() has only just started the process.

          The simplest for your case, if it suffices, is:

          p.start("sudo", { "-S" });
          p.write("1qaz ./live555ProxyServer -v rtsp://stream.com:554/Rz.32.02/");  # string might need a terminating `\n`, I don't know
          p.waitForFinished();
          qDebug() << p.readAll();
          
          M 1 Reply Last reply
          1
          • JonBJ JonB

            @Mihaill
            Now you need to look up how QProcess works. Your readAll() call is too early, the QProcess::start() has only just started the process.

            The simplest for your case, if it suffices, is:

            p.start("sudo", { "-S" });
            p.write("1qaz ./live555ProxyServer -v rtsp://stream.com:554/Rz.32.02/");  # string might need a terminating `\n`, I don't know
            p.waitForFinished();
            qDebug() << p.readAll();
            
            M Offline
            M Offline
            Mihaill
            wrote on last edited by
            #5

            @JonB I readAll() after 5 sec at start. I think that QProcess not work.

            JonBJ 1 Reply Last reply
            0
            • M Mihaill

              @JonB I readAll() after 5 sec at start. I think that QProcess not work.

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

              @Mihaill
              QProcess works fine.

              Why don't you try p.start("ls") or p.start("bash", { "-c", "ls" }) to see how that goes?

              Whether (a) your command line, (b) your sudo -S and (c) your ./live555ProxyServer command all behave as desired is a different matter.

              If you run sudo -S <<< 1qaz ./live555ProxyServer -v rtsp://stream.com:554/Rz.32.02/ on the command line, does it exit (i.e. immediately return to the shell prompt) or stay running (no return to shell prompt)?

              M 2 Replies Last reply
              0
              • JonBJ JonB

                @Mihaill
                QProcess works fine.

                Why don't you try p.start("ls") or p.start("bash", { "-c", "ls" }) to see how that goes?

                Whether (a) your command line, (b) your sudo -S and (c) your ./live555ProxyServer command all behave as desired is a different matter.

                If you run sudo -S <<< 1qaz ./live555ProxyServer -v rtsp://stream.com:554/Rz.32.02/ on the command line, does it exit (i.e. immediately return to the shell prompt) or stay running (no return to shell prompt)?

                M Offline
                M Offline
                Mihaill
                wrote on last edited by
                #7
                This post is deleted!
                1 Reply Last reply
                0
                • JonBJ JonB

                  @Mihaill
                  QProcess works fine.

                  Why don't you try p.start("ls") or p.start("bash", { "-c", "ls" }) to see how that goes?

                  Whether (a) your command line, (b) your sudo -S and (c) your ./live555ProxyServer command all behave as desired is a different matter.

                  If you run sudo -S <<< 1qaz ./live555ProxyServer -v rtsp://stream.com:554/Rz.32.02/ on the command line, does it exit (i.e. immediately return to the shell prompt) or stay running (no return to shell prompt)?

                  M Offline
                  M Offline
                  Mihaill
                  wrote on last edited by Mihaill
                  #8

                  @JonB I used m_proxyServerProcess.waitForFinished();
                  This code return normal result:

                      m_proxyServerProcess.start("/bin/bash", {"-c","echo 'some text' "});
                      m_proxyServerProcess.start("ls");
                      m_proxyServerProcess.start("/bin/bash", { "-c", "ls" });
                  

                  But it's return "" after 5 sec, but process not finished

                  m_proxyServerProcess.start("/bin/bash", {"-c",
                              "sudo -S <<< 1qaz /home/osm/Downloads/live/proxyServer/live555ProxyServer -v rtsp://stream.lesohranitel.ru:554/RU.32.02/ "});
                  
                  JonBJ 1 Reply Last reply
                  0
                  • M Mihaill

                    @JonB I used m_proxyServerProcess.waitForFinished();
                    This code return normal result:

                        m_proxyServerProcess.start("/bin/bash", {"-c","echo 'some text' "});
                        m_proxyServerProcess.start("ls");
                        m_proxyServerProcess.start("/bin/bash", { "-c", "ls" });
                    

                    But it's return "" after 5 sec, but process not finished

                    m_proxyServerProcess.start("/bin/bash", {"-c",
                                "sudo -S <<< 1qaz /home/osm/Downloads/live/proxyServer/live555ProxyServer -v rtsp://stream.lesohranitel.ru:554/RU.32.02/ "});
                    
                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #9

                    @Mihaill
                    If you want help answer questions asked. It's really not too hard. I last wrote:

                    If you run sudo -S <<< 1qaz ./live555ProxyServer -v rtsp://stream.com:554/Rz.32.02/ on the command line, does it exit (i.e. immediately return to the shell prompt) or stay running (no return to shell prompt)?

                    I asked that question because it is 100% vital to your question/behaviour/code. You know how your live555ProxyServer command behaves, how can I possibly know? Your ignoring it does not help either of us.

                    M 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @Mihaill
                      If you want help answer questions asked. It's really not too hard. I last wrote:

                      If you run sudo -S <<< 1qaz ./live555ProxyServer -v rtsp://stream.com:554/Rz.32.02/ on the command line, does it exit (i.e. immediately return to the shell prompt) or stay running (no return to shell prompt)?

                      I asked that question because it is 100% vital to your question/behaviour/code. You know how your live555ProxyServer command behaves, how can I possibly know? Your ignoring it does not help either of us.

                      M Offline
                      M Offline
                      Mihaill
                      wrote on last edited by
                      #10

                      @JonB In command line it's running.
                      I check this code and code works, but readAll() == "". It's strange.
                      Thanks for help.

                      JonBJ 1 Reply Last reply
                      0
                      • M Mihaill has marked this topic as solved on
                      • M Mihaill

                        @JonB In command line it's running.
                        I check this code and code works, but readAll() == "". It's strange.
                        Thanks for help.

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

                        @Mihaill said in How run bash script:

                        @JonB In command line it's running.

                        I don't known what that means.

                        Do you mean that after typing sudo -S <<< 1qaz /home/osm/Downloads/live/proxyServer/live555ProxyServer -v rtsp://stream.lesohranitel.ru:554/RU.32.02/ in a terminal the command prompt does not appear again and live555ProxyServer keeps running? I would like to help you but it's so difficult to get the answers out from you! :)

                        1 Reply Last reply
                        0

                        • Login

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