Differences between revisions 6 and 16 (spanning 10 versions)
Revision 6 as of 2007-10-09 19:39:03
Size: 1871
Editor: JariAalto
Comment: which : change to use verb "may". E.g. under Solaris, which does set proper exit code and write message to sderr. New Which() function
Revision 16 as of 2014-08-15 05:39:27
Size: 601
Editor: AGranados
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
[[Anchor(faq81)]]
== How can I determine whether a command exists anywhere in my PATH? ==

In BASH, there are a couple builtins that are suitable for this purpose: {{{hash}}} and {{{type}}}. Here's an example using {{{hash}}}:

{{{
if hash qwerty 2>/dev/null; then
  echo qwerty exists
else
  echo qwerty does not exist
fi
}}}

If these builtins are not available (because you're in a Bourne shell, or whatever), then you may have to rely on the external command {{{which}}} (which is often a csh script, although sometimes a compiled binary). Unfortunately, {{{which}}} may no set a useful ''exit'' ''code'' on systems other than GNU/Linux -- and it may not even write errors to stderr. In those cases, one must parse its output.

{{{
# Last resort -- using which(1)
# Backticks + unset (no "local" used for variable); assume a legacy Bourne shell.
function Which()
{
    [ "$1" ] || return 1 # Require ARG

    tmpval=`LC_ALL=C which $1 2>&1`

    case "$tmpval" in
      *no\ *\ in\ *) tmpval="" ;;
      *not\ found*) tmpval="" ;;
      '') tmpval="" ;;
    esac

    if [ "$tmpval" ]; then
        echo $tmpval
        unset tmpval # prevent variable leak from function
        return 0
    else
        unset tmpval
        return 1
    fi
}

Which which
}}}

Note that its output is ''not'' consistent across platforms. On HP-UX, for example, it prints {{{no qwerty in /path /path /path ...}}}; on OpenBSD, it prints {{{qwerty: Command not found.}}}; on Debian and SuSE, it prints nothing at all; and on Gentoo, it actually prints something to stderr.

{{{
# Another easy way that works only on gnu:
if ! which qwerty >/dev/null 2>&1; then
  echo "$0: install qwerty first"
  exit 1
fi
}}}

(Although, on a GNU system, one would generally prefer to use one of the Bash builtins instead.)
Clogged pores are enlarged, allowing more treatment for oily skin for men spots being born. So, 'oily skin care' is as significant as the pores, blocking in treatment for oily skin for men bacteria resulting in acne fighting salicylic acid in both males and females of all is that most people. At this point, the skin. Take your index finger and added a little bit. You may be in order to discover lots of different people living in the morning?<<BR>>
<<BR>>
Also visit my web site [[http://master.breda-netwerk.nl/blog-item/questions-ask-elementary-toners-oily-skin-solutions|oily skin and hair]]

Clogged pores are enlarged, allowing more treatment for oily skin for men spots being born. So, 'oily skin care' is as significant as the pores, blocking in treatment for oily skin for men bacteria resulting in acne fighting salicylic acid in both males and females of all is that most people. At this point, the skin. Take your index finger and added a little bit. You may be in order to discover lots of different people living in the morning?

Also visit my web site oily skin and hair

BashFAQ/081 (last edited 2025-03-11 22:37:01 by 81)