Maxpm
Maxpm

Reputation: 25572

How Do I Programmatically Check for a Program's Existence?

Let's say I'm writing something that depends on external programs, like svn. How do I check for their existence automatically, so I can print a helpful error message when they're absent? Iterating through PATH is possible, but hardly elegant and efficient. Are there cleaner solutions?

I've seen this behavior in a bootstrapping script, though I can't remember where. It looked a little like this:

checking for gcc... yes

Upvotes: 2

Views: 113

Answers (2)

9000
9000

Reputation: 40894

Try to actually call it.

It makes most sense to call it with -V or whatever else option that makes the program report its version; most of the time you want the program to be at least such-and-such version.

If your program is a shell script, which is your friend, too.

Upvotes: 0

Foo Bah
Foo Bah

Reputation: 26271

If you are using bash, you can use the type builtin:

$ type -f svn
svn is /usr/bin/svn

If you want to use it in a script:

$ type -f svn &>/dev/null; echo $?
0
$ type -f svn_doesnt_exist &>/dev/null; echo $?
1

Upvotes: 2

Related Questions