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 3.7k 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.
  • U Offline
    U Offline
    user4592357
    wrote on 2 Apr 2020, 17:39 last edited by
    #1

    i'm on linux, and i need to check whether a script exists.
    at first i used this code:

    auto sUtilName = "some_util";
    QFileInfo fileInfo(sUtilName);
    if (fileInfo.exists())
    	return sUtilName;
    

    actually, i can execute the script by running some_util on terminal, but qt doesn't find it.
    so i had to use full path of the script. but i'd first to check without full path, and if it isn't reachable, only then try full path.

    J 1 Reply Last reply 2 Apr 2020, 17:47
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 2 Apr 2020, 17:42 last edited by
      #2

      Hi,

      Where is that script supposed to be ?
      Where is your application located ?

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

      U 1 Reply Last reply 2 Apr 2020, 17:49
      1
      • U user4592357
        2 Apr 2020, 17:39

        i'm on linux, and i need to check whether a script exists.
        at first i used this code:

        auto sUtilName = "some_util";
        QFileInfo fileInfo(sUtilName);
        if (fileInfo.exists())
        	return sUtilName;
        

        actually, i can execute the script by running some_util on terminal, but qt doesn't find it.
        so i had to use full path of the script. but i'd first to check without full path, and if it isn't reachable, only then try full path.

        J Offline
        J Offline
        JonB
        wrote on 2 Apr 2020, 17:47 last edited by JonB 4 Feb 2020, 17:49
        #3

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

        actually, i can execute the script by running some_util on terminal, but qt doesn't find it.

        Is it that some_util is being found on your PATH (from your description this sounds quite likely)? Or (depending on your Linux flavor), if some_util is in the current directory when you type it in a terminal, what is the current directory in the terminal, and what is it when you run your Qt program?

        Please answer @SGaist's questions above.

        1 Reply Last reply
        1
        • S SGaist
          2 Apr 2020, 17:42

          Hi,

          Where is that script supposed to be ?
          Where is your application located ?

          U Offline
          U Offline
          user4592357
          wrote on 2 Apr 2020, 17:49 last edited by user4592357 4 Feb 2020, 17:50
          #4

          @SGaist
          i have some env var set, e.g. "export PROG_DIR=/some/path/" which isn't in $PATH, and the script is available at $PROG_DIR/some_util.
          application is located some directory, why does that matter?
          e.g. current dir=/path/, running aaplication: ./sub-dir/app

          J 1 Reply Last reply 2 Apr 2020, 17:53
          0
          • U user4592357
            2 Apr 2020, 17:49

            @SGaist
            i have some env var set, e.g. "export PROG_DIR=/some/path/" which isn't in $PATH, and the script is available at $PROG_DIR/some_util.
            application is located some directory, why does that matter?
            e.g. current dir=/path/, running aaplication: ./sub-dir/app

            J Offline
            J Offline
            JonB
            wrote on 2 Apr 2020, 17:53 last edited by JonB 4 Feb 2020, 17:58
            #5

            @user4592357
            If the current directory is /path/, and some_util is in $PROG_DIR (where $PROG_DIR is not /path/), going QFileInfo fileInfo("some_util"); is not going to find it. That will only look in whatever the current directory happens to be. And (unless you set it yourself) you don't know what it might be for an end user anyway; searching for a relative pathname is not good, unless you actually intend it only to look in the current directory.

            It sounds like you need to expand (the environment variable in) the $PROG_DIR/some_util to a full path if you expect to pass it to QFileInfo.

            U 1 Reply Last reply 2 Apr 2020, 17:57
            1
            • J JonB
              2 Apr 2020, 17:53

              @user4592357
              If the current directory is /path/, and some_util is in $PROG_DIR (where $PROG_DIR is not /path/), going QFileInfo fileInfo("some_util"); is not going to find it. That will only look in whatever the current directory happens to be. And (unless you set it yourself) you don't know what it might be for an end user anyway; searching for a relative pathname is not good, unless you actually intend it only to look in the current directory.

              It sounds like you need to expand (the environment variable in) the $PROG_DIR/some_util to a full path if you expect to pass it to QFileInfo.

              U Offline
              U Offline
              user4592357
              wrote on 2 Apr 2020, 17:57 last edited by
              #6

              @JonB
              ok, then why can i start the script with QProcess::startDetached(progName, args)?

              J C 2 Replies Last reply 2 Apr 2020, 18:00
              0
              • U user4592357
                2 Apr 2020, 17:57

                @JonB
                ok, then why can i start the script with QProcess::startDetached(progName, args)?

                J Offline
                J Offline
                JonB
                wrote on 2 Apr 2020, 18:00 last edited by
                #7

                @user4592357
                Because it's on your PATH?

                What is the "script" anyway? Is it something interpreted by something which looks at that PROG_DIR environment variable? I don't recognise what is using a PROG_DIR environment variable?

                U 1 Reply Last reply 2 Apr 2020, 18:03
                1
                • U user4592357
                  2 Apr 2020, 17:57

                  @JonB
                  ok, then why can i start the script with QProcess::startDetached(progName, args)?

                  C Offline
                  C Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 2 Apr 2020, 18:00 last edited by
                  #8
                  This post is deleted!
                  1 Reply Last reply
                  0
                  • J JonB
                    2 Apr 2020, 18:00

                    @user4592357
                    Because it's on your PATH?

                    What is the "script" anyway? Is it something interpreted by something which looks at that PROG_DIR environment variable? I don't recognise what is using a PROG_DIR environment variable?

                    U Offline
                    U Offline
                    user4592357
                    wrote on 2 Apr 2020, 18:03 last edited by user4592357 4 Feb 2020, 18:04
                    #9

                    @JonB
                    no the script isn't in $PATH.

                    ok, so at first script isn't available: bash: some_util: command not found
                    i set the env var: export PROG_DIR=/some/path/, now script is available: some_util
                    when i do which some_util, i get the value of $PROG_DIR, i.e. /some/path/

                    J 1 Reply Last reply 2 Apr 2020, 18:07
                    0
                    • C Offline
                      C Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on 2 Apr 2020, 18:06 last edited by
                      #10

                      PROG_DIR is no env var which evaluates something to be available in PATH later on so it must be something special in your setup.

                      1 Reply Last reply
                      2
                      • U user4592357
                        2 Apr 2020, 18:03

                        @JonB
                        no the script isn't in $PATH.

                        ok, so at first script isn't available: bash: some_util: command not found
                        i set the env var: export PROG_DIR=/some/path/, now script is available: some_util
                        when i do which some_util, i get the value of $PROG_DIR, i.e. /some/path/

                        J Offline
                        J Offline
                        JonB
                        wrote on 2 Apr 2020, 18:07 last edited by JonB 4 Feb 2020, 18:18
                        #11

                        @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 1 Reply Last reply 2 Apr 2020, 18:16
                        0
                        • J JonB
                          2 Apr 2020, 18:07

                          @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 2 Apr 2020, 18:16 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?

                          J 1 Reply Last reply 2 Apr 2020, 18:23
                          0
                          • U user4592357
                            2 Apr 2020, 18:16

                            @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?

                            J Offline
                            J Offline
                            JonB
                            wrote on 2 Apr 2020, 18:23 last edited by JonB 4 Feb 2020, 18:26
                            #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 2 Apr 2020, 18:31
                            1
                            • J JonB
                              2 Apr 2020, 18:23

                              @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 2 Apr 2020, 18:31 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))
                              ...
                              J 1 Reply Last reply 2 Apr 2020, 18:38
                              0
                              • C Offline
                                C Offline
                                Christian Ehrlicher
                                Lifetime Qt Champion
                                wrote on 2 Apr 2020, 18:33 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

                                1 Reply Last reply
                                2
                                • S Offline
                                  S Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on 2 Apr 2020, 18:35 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
                                    2 Apr 2020, 18:31

                                    @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))
                                    ...
                                    J Offline
                                    J Offline
                                    JonB
                                    wrote on 2 Apr 2020, 18:38 last edited by JonB 4 Feb 2020, 18:48
                                    #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 2 Apr 2020, 18:41 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.

                                      J 1 Reply Last reply 2 Apr 2020, 18:44
                                      0
                                      • U user4592357
                                        2 Apr 2020, 18:41

                                        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.

                                        J Offline
                                        J Offline
                                        JonB
                                        wrote on 2 Apr 2020, 18:44 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 2 Apr 2020, 18:54
                                        0
                                        • J JonB
                                          2 Apr 2020, 18:44

                                          @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 2 Apr 2020, 18:54 last edited by
                                          #20

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

                                          J 1 Reply Last reply 2 Apr 2020, 18:58
                                          0

                                          5/22

                                          2 Apr 2020, 17:53

                                          topic:navigator.unread, 17
                                          • Login

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