开发者

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开发者_JAVA技巧 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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜