UNIX — универсальная среда программирования - Керниган Брайан Уилсон
opath=$PATH PATH=/bin:/usr/bin
case $# in
0) echo 'Usage: which command' 1>&2; exit 2
esac
for i in `echo $opath | sed 's/^:/.:/
s/::/:.:/g
s/:$/:./
s/:/ /g'`
do
if test -f $i/$1 # this is /bin/test
then # or /usr/bin/test only
echo $i/$1
exit 0 # found it
fi
done
exit 1 # not found
3.8.67 wordfreq
awk ' { for (i = 1; i <= NF; i++) num[$i]++ }
END {for (word in num) print word, num[word] }
' $*
3.8.68 zap1
# zap pattern: kill all processes matching pattern
# BUG in this version
PATH=/bin:/usr/bin
case $# in
0) echo 'Usage: zap pattern' 1>&2; exit 1
esac
kill `pick `ps -ag | grep "$*"` | awk '{print $1}'`
3.8.69 zap2
# zap pat: kill all processes matching pat
# final version
PATH=/bin:/usr/bin
IFS='
' # just a newline
case $1 in
"") echo 'Usage: zap [-2] pattern' 1>&2; exit 1 ;;
-*) SIG=$1; shift
esac
echo ' PID TTY TIME CMD'
kill $SIG `pick `ps -ag | egrep "$*"|` | awk '{print $1}'`
3.8.70 zap.c
/* zap: interactive process killer */
#include <stdio.h>
#include <signal.h>
char *progname; /* program name for error message */
char *ps = "ps -ag"; /* system dependent */
main(argc, argv)
int argc;
char *argv[];
{
FILE *fin, *popen();
char buf[BUFSIZ];
int pid;
progname = argv[0];
if ((fin = popen(ps, "r")) == NULL) {
fprintf (stderr, "%s: can't run %sn", progname, ps);
exit(1);
}
fgets(buf, sizeof buf, fin); /* get header line */
fprintf (stderr, "%s", buf);
while (fgets(buf, sizeof buf, fin) != NULL)
if (argc == 1 || strindex(buf, argv[1]) >= 0) {
buf[strlen(buf)-1] = ' '; /* suppress n */
fprintf (stderr, "%s? ", buf);
if (ttyin() == 'y') {
sscanf(buf, "%d", &pid);
kill(pid, SIGKILL);
}
}
exit(0);
}
#include "ttyin2.c"
#include "strindex.c"
#include "efopen.c"