How would I validate that a program exists, in a way that will either return an error and exit, or continue with the script?
It seems like it should be easy, but it's been stumping me.
Join Stack Overflow to learn, share knowledge, and build your career.
|
|
AnswerPOSIX compatible:
For
ExplanationAvoid Why care?
So, don't use
(Minor side-note: some will suggest If your hash bang is If your script uses As a simple example, here's a function that runs
|
|||||||||||||||||||||
|
|
The following is a portable way to check whether a command exists in
Example:
The executable check is needed because bash returns a non-executable file if no executable file with that name is found in Also note that if a non-executable file with the same name as the executable exists earlier in In addition, this will fail if the command you are looking for has been defined as an alias. |
|||||||||||||||||||||
|
|
Script
Result
|
|||
|
|
|
|||||
|
|
Check for multiple dependencies and inform status to end users
Sample output:
Adjust the |
|||||
|
|
In case you want to check if a program exists and is really a program, not a bash built-in command, then For example, there is the time program which offers more features than the time built-in command. To check if the program exists, I would suggest using
|
|||
|
|
|
I'd say there's no portable and 100% reliable way due to dangling
Of course only the last one is problematic (no offence to Ringo!) But all of them are valid In order to reject dangling ones like Note that solution like this will unconditionally reject
|
|||||||||||||
|
|
Expanding on @lhunath's and @GregV's answers, here's the code for the people who want to easily put that check inside an
Here's how to use it:
|
|||||||||||||||||
|
|
I couldn't get one of the solutions to work, but after editing it a little I came up with this. Which works for me:
|
|||||
|
|
I use this because it's very easy:
or
It uses shell builtin and program echo status to stdout and nothing to stderr by the other hand if a command is not found, it echos status only to stderr. |
||||
|
|
|
If you guys can't get the things above/below to work and pulling hair out of your back, try to run the same command using First. It can give you completely different output.
Second. It can give you no output at all.
|
|||||||||||||||||||||
|
|
my setup for a debian server. i had a the problem when multiple packages contains the same name. for example apache2. so this was my solution.
|
|||
|
|
|
If there is no external
At least on Mac OS X 10.6.8 using Bash 4.2.24(2) |
||||
|
|
|
I agree with lhunath to discourage use of
Command Note: |
|||||||||||||||||||||
|
|
Also note that |
||||
|
|
|
I second the use of "command -v". E.g. like this:
|
|||
|
|
|
If you check for program existence, you are probably going to run it later anyway. Why not try to run it in the first place?
It's a more trustworthy check that the program runs than merely looking at PATH directories and file permissions. Plus you can get some useful result from your program, such as its version. Of course the drawbacks are that some programs can be heavy to start and some don't have a |
|||||
|
|
Why not use Bash builtins if you can?
...
|
|||||||||||||||||
|
|
I never did get the above solutions to work on the box I have access to. For one, type has been installed (doing what more does). So the builtin directive is needed. This command works for me:
|
|||||
|
|
Try using:
or
From the bash manpage under Conditional Expressions:
|
|||||||||
|
|
|||||
|
|
The hash-variant has one pitfall: On the command line you can for example type in
to have process executed. For this the parent folder of one_folder must be in $PATH. But when you try to hash this command, it will always succeed:
|
|||||
|
|
To use
This script runs
|
|||||
|
|
To mimic Bash's
|
|||||
|
|
I had to check if
Hope this help someone else! |
|||||||||
|
|
I have a function defined in my .bashrc that makes this easier.
Here's an example of how it's used (from my
|
|||||||||||||||||
|
|
For those interested, none of the methodologies above work if you wish to detect an installed library. I imagine you are left either with physically checking the path (potentially for header files and such), or something like this (if you are on a Debian-based distro):
As you can see from the above, a "0" answer from the query means the package is not installed. This is a function of "grep" - a "0" means a match was found, a "1" means no match was found. |
|||
|
It depends whether you want to know whether it exists in one of the directories in the
otherwise use
The redirection to |
|||||
|
|
The It returns 0 if the executable is found, 1 if it's not found or not executable:
Nice thing about which is that it figures out if the executable is available in the environment that which is run in - saves a few problems... -Adam |
|||||||||
|