How to test for an executable in the current path.

How to test for an executable in the current path.

am 02.10.2007 17:35:24 von Craig Sanders

Hello. I hope that someone can help me. I need to know how to check for the
presence of a particular binary executable file (pkg-config) in the current
PATH using BASH. For the life of me I can't think how to do it. If I execute
the following from a BASH command prompt ;

if test -x pkg-config; then echo "True"; else echo "False"; fi

then I get a result of False, even though pkg-config is in my PATH and I can
invoke it from the command line. However, if I execute the following from
the BASH command prompt ;

if test -x /usr/bin/pkg-config; then echo "True"; else echo "False"; fi

then I get a result of True.

This code needs to go into an Autoconf macro so I can't hardwire the
absolute path to pkg-config, as different users will have it installed in
different places.

Any help on this matter would be greatly appreciated.

- Craig

Re: How to test for an executable in the current path.

am 02.10.2007 17:46:31 von cfajohnson

On 2007-10-02, Craig Sanders wrote:
> Hello. I hope that someone can help me. I need to know how to check for the
> presence of a particular binary executable file (pkg-config) in the current
> PATH using BASH. For the life of me I can't think how to do it. If I execute
> the following from a BASH command prompt ;
>
> if test -x pkg-config; then echo "True"; else echo "False"; fi
>
> then I get a result of False, even though pkg-config is in my PATH and I can
> invoke it from the command line. However, if I execute the following from
> the BASH command prompt ;
>
> if test -x /usr/bin/pkg-config; then echo "True"; else echo "False"; fi
>
> then I get a result of True.
>
> This code needs to go into an Autoconf macro so I can't hardwire the
> absolute path to pkg-config, as different users will have it installed in
> different places.

All Bourne-type shells have 'type':

if type pkg-config; then ...

--
Chris F.A. Johnson, author
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence

Re: How to test for an executable in the current path.

am 02.10.2007 23:01:46 von Laurianne Gardeux

Am Wed, 03 Oct 2007 01:35:24 +1000 schrieb Craig Sanders:

> if test -x pkg-config; then echo "True"; else echo "False"; fi
>
> then I get a result of False, even though pkg-config is in my PATH and I
> can invoke it from the command line.

if pkg-config --version; then echo "True"; else echo "False"; fi


LG