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. Sourcing a script using QProcess
Forum Updated to NodeBB v4.3 + New Features

Sourcing a script using QProcess

Scheduled Pinned Locked Moved Unsolved General and Desktop
qprocesssourceshellscript
15 Posts 4 Posters 11.0k 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.
  • M Mark81
    14 Jan 2016, 11:10

    Hi! I want to execute a bash script from my application using QProcess. Because the script exports some env vars I need to "source" it. Example:

    cat myscript.sh
    #!/bin/sh
    echo Hello World
    export MYVAR=blablabla
    

    here my Qt application:

    QProcess p;
    p.start("myscript.sh");
    

    executes the script, but of course no vars are exported to the current shell. But:

    p.start("source myscript.sh");
    

    doesn't execute the script at all, despite no errors are returned. I said "doesn't execute" because waitForStarted() fails and "Hello world" isn't echoed.

    Where is my mistake?

    J Offline
    J Offline
    jsulm
    Lifetime Qt Champion
    wrote on 14 Jan 2016, 11:31 last edited by
    #3
    This post is deleted!
    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 14 Jan 2016, 11:34 last edited by
      #4

      Hi,

      I haven't tested source with QProcess but in any case the correct call would rather be:

      p.start("source", QStringList() << "/path/to/myscript.sh");
      

      Give the full path to myscript.sh. You are likely using a shadow build so myscript.sh will not be in the same folder as your application.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 1 Reply Last reply 14 Jan 2016, 11:49
      0
      • S SGaist
        14 Jan 2016, 11:34

        Hi,

        I haven't tested source with QProcess but in any case the correct call would rather be:

        p.start("source", QStringList() << "/path/to/myscript.sh");
        

        Give the full path to myscript.sh. You are likely using a shadow build so myscript.sh will not be in the same folder as your application.

        M Offline
        M Offline
        Mark81
        wrote on 14 Jan 2016, 11:49 last edited by
        #5

        @SGaist
        I tried like you suggested but the behavior is the same.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 14 Jan 2016, 12:33 last edited by
          #6

          Try with p.start("bash", QStringList() << "-c" << "\"\"source /path/to/test.sh\"\"");

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          M 2 Replies Last reply 14 Jan 2016, 13:21
          0
          • S SGaist
            14 Jan 2016, 12:33

            Try with p.start("bash", QStringList() << "-c" << "\"\"source /path/to/test.sh\"\"");

            M Offline
            M Offline
            Mark81
            wrote on 14 Jan 2016, 13:21 last edited by
            #7

            @mrjj
            In this way the process (bash, or /bin/sh in my case) starts but it immediately ends with exit value 2 without executing the script.
            QProcess returns no error.

            1 Reply Last reply
            0
            • S SGaist
              14 Jan 2016, 12:33

              Try with p.start("bash", QStringList() << "-c" << "\"\"source /path/to/test.sh\"\"");

              M Offline
              M Offline
              Mark81
              wrote on 14 Jan 2016, 13:27 last edited by
              #8

              @SGaist
              The process runs and the exit code is 0, but the script is not executed. By the way if I try to execute the following on prompt:

              /bin/sh -c ""source /path/to/test.sh""
              

              it does "nothing" (I mean no errors nor echo output).

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 14 Jan 2016, 13:43 last edited by
                #9

                How are you checking that it does nothing with QProcess ?

                In the console version you have too much quotes it should only be bash -c "source /path/to/test.sh"

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                M 1 Reply Last reply 14 Jan 2016, 14:29
                0
                • S SGaist
                  14 Jan 2016, 13:43

                  How are you checking that it does nothing with QProcess ?

                  In the console version you have too much quotes it should only be bash -c "source /path/to/test.sh"

                  M Offline
                  M Offline
                  Mark81
                  wrote on 14 Jan 2016, 14:29 last edited by
                  #10

                  @SGaist

                  How are you checking that it does nothing with QProcess ?
                  

                  The env vars are not exported and there is no echo.

                  In the console version you have too much quotes it should only be bash -c "source /path/to/test.sh"
                  

                  Yes, in this way the echo is shown but the vars are not exported. It's the same as run the script directly without sourcing it. But it's beyond my knowledge to understand why.

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 14 Jan 2016, 14:35 last edited by
                    #11

                    Can I ask silly question?
                    If you run bash first, wont the export be in that instance of bash?
                    source test.sh;echo $MYVAR
                    does as expected
                    but not with bash first.

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 14 Jan 2016, 21:54 last edited by
                      #12

                      On OS X putting the echo call inside the "command string" worked.

                      I'm reading stdout using readAllStandardOutput

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      M 1 Reply Last reply 15 Jan 2016, 07:50
                      0
                      • S SGaist
                        14 Jan 2016, 21:54

                        On OS X putting the echo call inside the "command string" worked.

                        I'm reading stdout using readAllStandardOutput

                        M Offline
                        M Offline
                        Mark81
                        wrote on 15 Jan 2016, 07:50 last edited by
                        #13

                        @SGaist
                        Ok, I will try using readAllStandardOutput() too. But also the env vars are correctly exported?

                        1 Reply Last reply
                        0
                        • J Offline
                          J Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on 15 Jan 2016, 07:55 last edited by
                          #14

                          You can set the environment using

                          QProcess::setProcessEnvironment(const QProcessEnvironment & environment)
                          

                          You can get the environment of your current process via

                          QProcessEnvironment::systemEnvironment()
                          
                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on 15 Jan 2016, 22:39 last edited by
                            #15

                            @Mark81 Yes both the Hello World and content of the variable were shown. Again it's on OS X with a recent version of bash.

                            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
                            0

                            12/15

                            14 Jan 2016, 21:54

                            • Login

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