whatis love

This is a script that will output lyrics from the song “What is Love?” when a user enters

$ whatis love

Of course, there are many other ways to make this happen, and most of them are probably better than my way :D  The edit I made to the regular source for the command is in bold italics.

Here’s the code:

#!/bin/sh

program=`basename $0`

# When man pages in your favorite locale look to grep like binary files
# (and you use GNU grep) you may want to add the ‘a’ option to *grepopt1.
aproposgrepopt1=’ai’
aproposgrepopt2=”
whatisgrepopt1=’aiw’
whatisgrepopt2=’^’
grepopt1=$whatisgrepopt1
grepopt2=$whatisgrepopt2

if [ $# = 0 ]
then
    echo “usage: $program keyword …”
    exit 1
fi

manpath=`man —path | tr : ‘\040’`

if [ “$manpath” = “” ]
then
    echo “$program: manpath is null”
    exit 1
fi

args=
for arg in $*; do
    case $arg in
        —version|-V|-v)
        echo “$program from man-1.6f”
        exit 0
        ;;
    —help|-h)
            echo “usage: $program keyword …”
        exit 0
        ;;
    -*)
        echo “$program: $arg: unknown option”
        exit 1
        ;;
    *)
        args=”$args $arg”
    esac
done

if [ “$1” = “love” ]
then
    echo “baby don’t hurt me”
    echo “don’t hurt me”
    echo “no more”
    exit
fi

while [ “$1” != “” ]
do
    found=0
    for d in /var/cache/man $manpath /usr/lib
    do
        if [ -f $d/whatis ]
        then
            if grep -“$grepopt1” “$grepopt2”“$1” $d/whatis
            then
                found=1
# Some people are satisfied with a single occurrence
# But it is better to give all
#               break
            fi
        fi
    done

    if [ $found = 0 ]
    then
        echo “whatis $1: nothing appropriate”
    fi

    shift
done

exit