Tags: binary, current, executable, file, linux, particular, path, pkg-config, programming, thepresence, unix

How to test for an executable in the current path.

On Programmer » Unix & Linux

2,685 words with 2 Comments; publish: Mon, 28 Apr 2008 07:15:00 GMT; (20062.50, « »)

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

All Comments

Leave a comment...

  • 2 Comments
    • On 2007-10-02, Craig Sanders wrote:

      > Hello. I hope that someone can help me. I need to know how to check for th

      e

      > presence of a particular binary executable file (pkg-config) in the curren

      t

      > PATH using BASH. For the life of me I can't think how to do it. If I execu

      te

      > 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 c

      an

      > 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 <http://cfaj.freeshell.org/shell/>

      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

      #1; Mon, 28 Apr 2008 07:16:00 GMT
    • 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

      #2; Mon, 28 Apr 2008 07:17:00 GMT