[comp.unix.questions] sh script question

rrg@fibercom.COM (Rhonda Gaines) (04/11/91)

I am trying to edit the following script to check if a man page has been
compressed and if so to pipe it through zcat first and then eroff it.
The problem is that sh is not recognizing the *.Z as a wildcard.  Any ideas?
I'm pretty new at this so any help will be appreciated.

 -rhonda


#! /bin/sh

#
#	Usage: lpman <command> [section]
#
#	Print hardcopy of online manual pages for <command> in [section].
#	If section is not given, print all instances of <command> found.
#	If <command> is a string enclosed with double-quotes, any wildcard
#	characters will be expanded as the search for man pages is made
#	(e.g. 'lpman "init*"' will print all instances of man pages starting
#	with the string "init").
#
#	$1 = manual entry
#	$2 = section number

FILES=`find /usr/man/man*$2* -name "$1.*" -print`
if test "$FILES" = "" ; then
	echo -n "No manual entry for $1"
	if test "$2" = "" ; then
		:
	else
		echo -n " in section ($2)"
	fi
	echo
else
	if test "$FILES" = "*.Z" ; then
		echo -n "$1 is compressed, piping output through zcat"
		/usr/ucb/zcat $FILES|/usr/local/bin/eroff -man $FILES
	else
		/usr/local/bin/eroff -man $FILES
		echo -n "Manual page for $1"
	fi
	if test "$2" = "" ; then
		:
	else
		echo -n " ($2)"
	fi
	echo " sent to LaserWriter"
fi

jik@athena.mit.edu (Jonathan I. Kamens) (04/13/91)

In article <16387@fibercom.COM>, rrg@fibercom.COM (Rhonda Gaines) writes:
|> I am trying to edit the following script to check if a man page has been
|> compressed and if so to pipe it through zcat first and then eroff it.
|> The problem is that sh is not recognizing the *.Z as a wildcard.  Any ideas?
|> I'm pretty new at this so any help will be appreciated.

  I see several problems with your script.

  The first is here:

|> if test "$FILES" = "" ; then

This isn't really a problem, more of a stylistic thing, but I would use the
"-z" option to test to see if the string is zero-length, i.e.:

	if test -z "$FILES" ; then

Same thing here:

|> 	if test "$2" = "" ; then

I would use:

	if test -z "$2" ; then

  Here's the first real problem:

|> 	if test "$FILES" = "*.Z" ; then

First of all, the "test" command doesn't support file wildcard matching, which
is what you're trying to get it to do.  You probably want to use something
like this:

	if test `expr "$FILES" : ".*\.Z"` -gt 0 ; then

However, this isn't really enough either.  Remember, your "find" command above
may find *one or more files* that match the name specified to the shell
script.  This means that the FILES variable may contain one or more files, but
your entire script (including this check, and the zcat and eroff commands
later in the script) are treating the variable as if it only contains one
file.  You probably won't have any trouble if all of the files in FILES are
compressed or if they all aren't compressed, but if some of them are
compressed and some aren't then things will go badly.

  What you want to do is replace this line in your script:

|> FILES=`find /usr/man/man*$2* -name "$1.*" -print`

With this:

	for FILES in `find /usr/man/man*$2* -name "$1.*" -print` ; do

and then add

	done

at the bottom of the script.  This will deal with each file, checking whether
or not it is compressed, individually.

  I hope all this helps.

-- 
Jonathan Kamens			              USnail:
MIT Project Athena				11 Ashford Terrace
jik@Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-8085			      Home: 617-782-0710