2

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
2
  • When you say "program" exactly what do you mean? A binary file? Are you talking about linux? Commented Oct 5, 2011 at 0:04
  • @MostyMostacho Anything callable via system() in C or via the command line, basically. And yes, Linux/Unix. (See tag.) Commented Oct 5, 2011 at 0:11

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

any benefit of this over which? That's what I'd normally use.
@SanjayManohar which is a program, but type is a shell builtin. Furthermore, type can also find stuff like functions and can cut through aliases. For example, if you alias a command, type will tell you the alias whereas which will fail to find anything.
0

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.

2 Comments

-1: Definitely don't blindly run commands unless you are absolutely sure that running the program is not deleterious.
@FooBah: If you don't have access to package system, etc, you can only see if an executable of given name is present on PATH or not. An attempt to execute will search PATH anyway, and either run the command or return an error. What security or robustness will you buy doing this by hand?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.