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. check whether a script exists by script name
Forum Updated to NodeBB v4.3 + New Features

check whether a script exists by script name

Scheduled Pinned Locked Moved Solved General and Desktop
22 Posts 4 Posters 4.3k 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.
  • JonBJ JonB

    @user4592357
    I will look around for PROG_DIR now, because I'm interested. EDIT Couldn't find it anywhere!

    Meanwhile, for your question, if some_util is not going to be in the current directory, then QFileInfo("some_util") is not going to find it. If it is found in $PROG_DIR then you will need to expand that environment variable in your code before passing to QFileInfo.

    OOI: can you show us what your PATH environment is before you do anything in a new terminal/shell? From what you described: Does bash allow $PROG_DIR to be "embedded" as an environment variable inside PATH??

    U Offline
    U Offline
    user4592357
    wrote on last edited by
    #12

    @JonB
    okay i will use full path, that not a problem.

    i have one more question, so that script actually outputs something, i'm calling it with QProcess::startDetached(progName, args) where args is the args for the script.
    but since it has output, i wanna suppress it, so i added /dev/null:

    const auto args = QStringList() <<
    		... <<
    		"> /dev/null 2>&1";
    

    but it is passed to the script and the script says it's an unknown/unexpected argument. what should i do?

    JonBJ 1 Reply Last reply
    0
    • U user4592357

      @JonB
      okay i will use full path, that not a problem.

      i have one more question, so that script actually outputs something, i'm calling it with QProcess::startDetached(progName, args) where args is the args for the script.
      but since it has output, i wanna suppress it, so i added /dev/null:

      const auto args = QStringList() <<
      		... <<
      		"> /dev/null 2>&1";
      

      but it is passed to the script and the script says it's an unknown/unexpected argument. what should i do?

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

      @user4592357
      Once you start using symbols like >, 2>&1 you now cannot try to execute some_util directly. Those are interpreted by the (bash) shell. You will have to achieve a:

      bash -c 'some_util ... > /dev/null 2>&1'
      

      so the executable is bash with two arguments, -c and some_util ... > /dev/null 2>&1 as a single-argument-string.

      This is getting hairy, and probably not the best way to do it. Better use QProcess stdout/err redirection if that's what you need to achieve.

      U 1 Reply Last reply
      1
      • JonBJ JonB

        @user4592357
        Once you start using symbols like >, 2>&1 you now cannot try to execute some_util directly. Those are interpreted by the (bash) shell. You will have to achieve a:

        bash -c 'some_util ... > /dev/null 2>&1'
        

        so the executable is bash with two arguments, -c and some_util ... > /dev/null 2>&1 as a single-argument-string.

        This is getting hairy, and probably not the best way to do it. Better use QProcess stdout/err redirection if that's what you need to achieve.

        U Offline
        U Offline
        user4592357
        wrote on last edited by
        #14

        @JonB
        yes, i need to not have output from that script i'm calling. this didn't work, i still get the output:

        QProcess process;
        process.setStandardOutputFile(QProcess::nullDevice());
        if (!process.startDetached(progName, args))
        ...
        JonBJ 1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #15

          @user4592357 said in check whether a script exists by script name:

          !process.startDetache

          QProcess::startDetached() is a static function - if you need something from the process, you have to use QProcess::start() as explained in the documentation

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          2
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #16

            You're using this static overload which is not the same as this member function.

            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
            1
            • U user4592357

              @JonB
              yes, i need to not have output from that script i'm calling. this didn't work, i still get the output:

              QProcess process;
              process.setStandardOutputFile(QProcess::nullDevice());
              if (!process.startDetached(progName, args))
              ...
              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #17

              @user4592357
              In addition to my learned colleague @Christian-Ehrlicher's excellent catch :)
              You also need/should call setStandardErrorFile() (or use MergedChannels), in addition to your Output one.

              1 Reply Last reply
              0
              • U Offline
                U Offline
                user4592357
                wrote on last edited by
                #18

                thanks for the replies, now i have this and it works. is this code okay?

                	QProcess process;
                	process.closeReadChannel(QProcess::StandardOutput);
                	process.start(progPath, args);
                	while (!process.atEnd());
                	process.close();
                

                also, why suppress errors? i'd like to see if the called program has any errors.

                JonBJ 1 Reply Last reply
                0
                • U user4592357

                  thanks for the replies, now i have this and it works. is this code okay?

                  	QProcess process;
                  	process.closeReadChannel(QProcess::StandardOutput);
                  	process.start(progPath, args);
                  	while (!process.atEnd());
                  	process.close();
                  

                  also, why suppress errors? i'd like to see if the called program has any errors.

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

                  @user4592357 said in check whether a script exists by script name:

                  also, why suppress errors? i'd like to see if the called program has any errors.

                  You wrote

                  but since it has output, i wanna suppress it

                  and the code you chose to show had

                  "> /dev/null 2>&1"
                  

                  I was just translating what you wrote, where you are sending stderr to nowhere. It's up to you.

                  U 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @user4592357 said in check whether a script exists by script name:

                    also, why suppress errors? i'd like to see if the called program has any errors.

                    You wrote

                    but since it has output, i wanna suppress it

                    and the code you chose to show had

                    "> /dev/null 2>&1"
                    

                    I was just translating what you wrote, where you are sending stderr to nowhere. It's up to you.

                    U Offline
                    U Offline
                    user4592357
                    wrote on last edited by
                    #20

                    @JonB
                    okay. and above code is okay? specifically, the while (!process.atEnd()); part.

                    JonBJ 1 Reply Last reply
                    0
                    • U user4592357

                      @JonB
                      okay. and above code is okay? specifically, the while (!process.atEnd()); part.

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

                      @user4592357

                      while (!process.atEnd());

                      Lord, no, I didn't look! That would occupy all CPU time for parent process!

                      If you want to do it synchronously that way you must at least use waitForFinished(), see the example & discussion at https://doc.qt.io/qt-5/qprocess.html#synchronous-process-api. It's not the best compared to asynchronous with signal/slot, but I haven't got time, again see the doc if you want to understand.

                      U 1 Reply Last reply
                      2
                      • JonBJ JonB

                        @user4592357

                        while (!process.atEnd());

                        Lord, no, I didn't look! That would occupy all CPU time for parent process!

                        If you want to do it synchronously that way you must at least use waitForFinished(), see the example & discussion at https://doc.qt.io/qt-5/qprocess.html#synchronous-process-api. It's not the best compared to asynchronous with signal/slot, but I haven't got time, again see the doc if you want to understand.

                        U Offline
                        U Offline
                        user4592357
                        wrote on last edited by
                        #22

                        @JonB
                        i see. thanks a lot

                        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