To me a better check is to run the program at the beginning of the script (with -V
say).
I'd use the same invocation as you use to run the job later (via shell or not, via execvp
). Once at it, make sure to see whether it threw errors. This is also discussed in your link but I would in fact get the output back (not send it away) and check that. This is the surest way to see whether the thing actually runs out of your program and whether it is what you expect it to be.
Checking for the executable with -x
(if you know the path) is useful, too, but it only tells you that a file with a given name is there and that it is executable.
The system's which
seems to be beset with critism for its possible (mis)behavior, it may or may not be a shell-builtin (which complicates how exactly to use it), is an external utility, and its exact behavior is system dependent. The module File::Which
pointed out in Borodin's answer would be better -- if it is indeed better than which
. (What it may well be, I just don't know.)
Note. I am not sure what "bash command" means: a bash shell built-in, or the fact that you use bash when on terminal? Perl's qx
and system
use the sh
shell, not bash
(if they invoke the shell, which depends on how you use them). While sh
is mostly a link, and often to bash
, it may not be and there are differences, and you cannot rely on your shell configuration.
Can also actually run a shell, qx(/path/bash -c 'cmd args')
, if you must. Mind the quotes. You may need to play with it to find the exact syntax on your system. See this page and links.