I'm trying to write all of my sh startup/env scripts to work with as much DRY and as much: "works on every *nix I clone it to", as possible. This means making sure that if I try to run code that's not there, that the code fails gracefully. To that end I need to be able to test if programs exist. I know how to test if a file exists, but I'm not sure how to test to see if an application is executable within the path. I'd rather use the $PATH, as some of these need to work on arch, ubuntu, and centos. Some might be installed in my homedir, on systems where I don't have root, others might not be installed, and others yet my be installed in system paths.
|
|
||||
|
|
Use
There are a few sh variants (definitely pre-POSIX; I know of
This function is generally useful if you want to exclude built-ins and functions and look up the name in
Ksh, bash and zsh also have |
|||||||||||||
|
|
If you are only looking for external programs, you can also use which. Don't know how portable that is though. |
|||||
|