A2osX/BIN/WHICH.txt
2023-02-09 13:10:22 -05:00

77 lines
1.3 KiB
Bash

#!/bin/sh
#
# which
#
# locates a program in path
#
set -E
if [ -z $1 ] or [ $1 = "-h" ] or [ $1 = "-H" ]
echo "Usage : which [options] program ..."
echo " -A show all found"
echo " -S no output, 0 if found, 1 if not"
exit 120
fi
set T = "${TEMP}which$$.tmp"
set A = 0
set F = 0
set S = 0
set L = 1
# get options
while [ ${L} -eq 1 ]
if [ $1 = "-a" ] or [ $1 = "-A" ]
set A = 1
shift
else
if [ $1 = "-s" ] or [ $1 = "-S" ]
set S = 1
shift
else
set L = 0
fi
fi
loop
# split dirs in path
echo "${PATH}:." > ${T}
set L = `sed "s/:/ /" ${T}`
rm -q -c ${T}*
# find instances
while [ $# -gt 0 ]
set F = ${F} + 1
for D in ${L}
# check for file
if [ -f ${D}$1 ]
set T = 0
else
set T = 1
fi
# process result
if [ ${T} -eq 0 ]
if [ ${S} -eq 0 ]
echo "${D}$1"
fi
set F = ${F} - 1
if [ ${A} -eq 0 ]
break
fi
fi
next
shift
loop
# -s numeric result
if [ ${S} -gt 0 ]
if [ ${F} -eq 0 ]
exit 0
else
exit 70
fi