shell script implementation of man

Matthew Hiles matthew.hiles at gmail.com
Fri Nov 21 16:43:48 UTC 2008


On Fri, Nov 21, 2008 at 2:29 AM, Natanael Copa <natanael.copa at gmail.com> wrote:
> On Wed, 2008-11-19 at 12:54 -0500, Matthew Hiles wrote:
>
>> [ -z $PAGER ] && PAGER=less
>
> you need quotes:
>
> [ -z "$PAGER" ] && PAGER=less
>
>
>> #okay, found the page, try decompressing and displaying
>> case $pagefile in
>> *.bz2)
>>        bzcat $pagefile | $PAGER ;;
>> *.gz)
>>        zcat $pagefile | $PAGER ;;
>> *)
>>        $PAGER $pagefile ;;
>> esac
>
> probably a good idea to quote "$pagefile" and "$PAGER" as well.
>
> PAGER="/path with spasecs/myless"
>
> -nc
>
>

I assumed no one would be using spaces because spaces in the MANPATH
would break the script anyway. O:-D

But I did go ahead and fix a lot of my variables.  As far as where to
put this script in the distro, I thought there was an extras directory
for this sort of thing, (and if there isn't, shouldn't there be?).

I have two versions now, one with a few more comments and one without.
Here's the latest one with more of the suggested improvements/fixes
and no comments (aside from the legal header and requirements). It's
987 bytes and works with busybox's bourne shell and msh.


#!/bin/sh

# man - a simple man program for ASCII pages
# Copyright (c) 2008 Matthew Hiles
#
# Licensed under GPLv2 or later, see file LICENSE in this tarball for details.

## requires: find, head, sort, tr, cat, and grep/egrep
## optional: zcat, bzcat, less

if [ -z "$MANPATH" ]; then
        echo "Warning: MANPATH is not set, assuming /usr/share/man." >&2
        MANPATH=/usr/share/man
fi

case $# in
1)
        pagearg=$1 ;;
2)
        section=man$1
        pagearg=$2 ;;
*)
        echo "Usage: man [section] <manpage>"
        exit
esac

paths=`echo "$MANPATH" | tr ":" " "`
pagefile=`find $paths | grep "/$section" | grep "/$pagearg\." | sort | head -n1`

if [ -z "$pagefile" ]; then
        echo -n No manual entry for $pagearg
        [ $section ] && echo -n " in section $1 of the manual."
        echo
        exit
fi

[ "$PAGER" ] || PAGER=less
tty -s <&1 || PAGER=cat

case "$pagefile" in
*.bz2)
        bzcat "$pagefile" | "$PAGER" ;;
*.gz)
        zcat "$pagefile" | "$PAGER" ;;
*)
        "$PAGER" "$pagefile" ;;
esac

paths=
pagefile=
pagearg=
section=



More information about the busybox mailing list