diff --git a/BIN/WHICH.txt b/BIN/WHICH.txt new file mode 100644 index 00000000..cdbd994b --- /dev/null +++ b/BIN/WHICH.txt @@ -0,0 +1,77 @@ +NEW + PREFIX +AUTO 4,1 +#!/bin/sh +# +# which +# +# locates a program in path +# + +set -E + +if [ -z $1 ] + echo "USAGE: which program ..." + 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 [ ${S} -eq 0 ] + ls -f ${D}$1 + set T = $? + else + ls -f ${D}$1 > /dev/null + set T = $? + fi + + # process result + if [ ${T} -eq 0 ] + 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 + +MAN +TEXT /BIN/which diff --git a/HELP/which.txt b/HELP/which.txt new file mode 100644 index 00000000..18970e40 --- /dev/null +++ b/HELP/which.txt @@ -0,0 +1,44 @@ +TITLE +A2osX which (bin/which) Command Help + + which -- locate a program file in the user's path + +SYNOPSIS + which program ... + +DESCRIPTION + The which utility takes a list of command names and searches the path for + each and will report back which specific executable would have been picked + up if the command was actually run. + + The which utility accepts the following options: + + -a List all instances found instead of just the first one. + + -s No output, just return 0 if all programs are found, or 1 if + some were not found. + +PAGE +EXAMPLES + Locate the cp and ls commands: + + # which cp ls + /FULL32/bin/cp + /FULL32/bin/ls + + Do not show output, just return code: + + # which -s seq + # echo $? + 0 + + # which -s fakecmd + # echo $? + 1 + + +HISTORY + The which command first appeared in FreeBSD 2.1 and was a Perl script + written by Wolfram Schneider . The A2osX which command + was developed by Brian J. Bernstein in Dec 2022. +