initial commit of 'which' utility and help page.

This commit is contained in:
Brian J. Bernstein 2022-12-02 13:14:00 -05:00
parent c6cdd2d10b
commit 8bd9dbd4e0
2 changed files with 121 additions and 0 deletions

77
BIN/WHICH.txt Normal file
View File

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

44
HELP/which.txt Normal file
View File

@ -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 <wosch@FreeBSD.org>. The A2osX which command
was developed by Brian J. Bernstein <brian@dronefone.com> in Dec 2022.