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.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.
  • 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()
                    

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    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