aboutsummaryrefslogtreecommitdiff
path: root/shunit2-2.0.3/bin/which
blob: 9d7cc9ffabbf02a29926a37e8b75817ee41bfb60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#! /bin/sh
# $Id$
#
# This is a simple implementation of the 'which' command for those OSes that
# don't have one.
#

true; TRUE=$?
false; FALSE=$?

showAll=${FALSE}

# process command line flags
while getopts 'a' opt; do
  case ${opt} in
    a) showAll=${TRUE}
  esac
done
shift `expr ${OPTIND} - 1`

# exit if no arguments were given
[ $# -eq 0 ] && exit 1

command=$1

# search for command
out=`echo "${PATH}" |sed "s/:/\n/g" |\
while read path; do
  fullPath="${path}/${command}"
  if [ -x "${fullPath}" ]; then
    echo "${fullPath}"
    [ ${showAll} -eq ${FALSE} ] && break
  fi
done`
[ -z "${out}" ] && exit 1
echo "${out}"