[Hampshire] Checking command existence, host OS independent …

Top Page

Reply to this message
Author: Richard Danter
Date:  
To: Hampshire LUG Discussion List
Subject: [Hampshire] Checking command existence, host OS independent way..?
Hi folks,

I have been using a trivial test to discover if a command is available
on a given host for some time as follows:

[ -n "`which _command_`" ] && _do_something_with_command_

This works perfectly on Linux, FreeBSD and NetBSD but not on Solaris.

Sun decided it would be useful if "which" returned all the places it
looked (i.e. your path) if it could not find what you were looking for.
Since the result is now not an empty string the test fails to identify
that the command was not found.

Is there a better way to do this that will work for all UNIX flavours?

Has to work in Bourne (both original, dash and bash) shells and not rely
on any other tools which may not exist on all machines.

It will probably end up being something like this I guess:

if `which _command_` == "" OR contains "not found" then...

Thanks
Rich

--
http://radanter.deviantart.com/