check whether a script exists by script name
-
@user4592357
If the current directory is/path/
, andsome_util
is in$PROG_DIR
(where$PROG_DIR
is not/path/
), goingQFileInfo 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 toQFileInfo
. -
@user4592357
Because it's on yourPATH
?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 aPROG_DIR
environment variable? -
This post is deleted!
-
@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 dowhich some_util
, i get the value of$PROG_DIR
, i.e./some/path/
-
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.
-
@user4592357
I will look around forPROG_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, thenQFileInfo("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 toQFileInfo
.OOI: can you show us what your
PATH
environment is before you do anything in a new terminal/shell? From what you described: Doesbash
allow$PROG_DIR
to be "embedded" as an environment variable insidePATH
?? -
@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)
whereargs
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?
-
@user4592357
Once you start using symbols like>
,2>&1
you now cannot try to executesome_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
andsome_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. -
@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
-
You're using this static overload which is not the same as this member function.
-
@user4592357
In addition to my learned colleague @Christian-Ehrlicher's excellent catch :)
You also need/should callsetStandardErrorFile()
(or useMergedChannels
), in addition to yourOutput
one. -
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.
-
@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.
-
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.