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

Sourcing a script using QProcess

Scheduled Pinned Locked Moved Unsolved General and Desktop
qprocesssourceshellscript
15 Posts 4 Posters 10.9k 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.
  • mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by mrjj
    #2

    Hi
    is source a standalone program/command ?
    else it wont work as QProcess
    have no idea how to run it
    if internal bash command.
    ( I think )
    maybe:
    Process.start("bash", QStringList() << "-c" << "source" << "myscript.sh ");

    1 Reply Last reply
    0
    • M Mark81

      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?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #3
      This post is deleted!
      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on 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
        0
        • SGaistS SGaist

          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 last edited by
          #5

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

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on 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
            0
            • SGaistS SGaist

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

              M Offline
              M Offline
              Mark81
              wrote on 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
              • SGaistS SGaist

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

                M Offline
                M Offline
                Mark81
                wrote on 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
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 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
                  0
                  • SGaistS SGaist

                    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 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
                    • mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 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
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 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
                        0
                        • SGaistS SGaist

                          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 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
                          • jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on 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
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on 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

                              • Login

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