How would I validate that a program exists?
Which would then either return an error and exit or continue with the script?
It seems like it should be easy, but it's been stumping me.
|
Yes; avoid 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
In summary:Where When writing a POSIX script, use |
|||||||||||||||||||||
|
I agree with lhunath to discourage use of
Command Note: |
|||||||||||||||||
|
I have a function defined in my .bashrc that makes this easier.
Here's an example of how it's used (from my
|
|||||||||||||||||
|
It depends whether you want to know whether it exists in one of the directories in the
otherwise use
The redirection to |
|||||
|
The following is a portable way to check whether a command exists in
Example:
Although if it's not executable, it may be better to special-case that, as that probably indicates a more serious issue. |
|||
|
To use
This script runs
|
|||||
|
Try using:
or
From the bash manpage under Conditional Expressions:
|
|||||||||
|
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 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:
|
||||
|
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 |
|||||
|
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 |
|||||||||
|
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. |
|||
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:
|
|||||
|
Why not use Bash builtins if you can?
...
|
|||||||||
|
To mimic Bash's
|
|||||
|
I second the use of "command -v". E.g. like this:
|
|||
|
Also note that |
||||
|
If there is no external
At least on Mac OS X 10.6.8 using Bash 4.2.24(2) |
||||
|
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.
|
|||||||||||||||||||||
|
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. |
||||
|
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
|
|||||||||
|
Script copy paste to check for multiple dependencies and inform status to end users:
Sample output:
Adjust the |
|||||
|
I had to check if
Hope this help someone else! |
|||||||||
|
|
|||||
|
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.
|
|||
|
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 couldn't get one of the solutions to work, but after editing it a little I came up with this. Which works for me:
|
|||||
|