views:

95

answers:

6

I'm building a small shell script for finding cat/man pages on a wide range of unix systems... in bash, I could build all possible paths by doing this:

# default search paths
default=$(echo /usr/{share, local, dpkg, XR11}/man/{man, cat}{1..8})

for path in $default
do 
  ...
done

Unfortunately, I'm forced to use sh... I could build the paths with loops, but this would look really ugly... is there a neater/shorter way?

+2  A: 

On my Linux system, "man -w" prints the location of the nroff source file instead of its contents. That way you use man's internal search to find the files - just like a user on the command line.

See "man man" for more information.

Robert Wohlfarth
Uh, didn't see that option, thx ^^
Trollhorn
That works fine as long as there's nowhere that 'man' doesn't know about that you need it to look. Then you start playing with MANPATH.
Jonathan Leffler
A: 

Nope, doesn't work for me... if i use

#!/bin/sh

it doesn't do any brace expansions :-(

Trollhorn
Welcome to SO! You should delete this answer and post it as a comment to another answer or edit your question to include it, as appropriate.
Roger Pate
A: 

Given Bourne shell, I'd probably use:

for path in `ls -d /usr/*/man/[mc]a[tn][1-8]`
do 
    if [ -d $path ]
    then ...
    fi
done

This only leads you astray if somebody malicious creates a directory /usr/spoof/man/man3, or /usr/share/man/mat1 or /usr/dpkg/man/can2, which is unlikely enough that I'd not worry about it.

Jonathan Leffler
+1  A: 

This isn't terrible:

for dir in share local pdkg XR11; do
  for type in man cat; do
    for n in 1 2 3 4 5 6 7 8; do
      path="/usr/${dir}/man/${type}$n"
      # ...
    done
  done
done

or even, though it's not DRY, this is explicit and readable

prefixes="
    /usr/share/man/man /usr/share/man/cat
    /usr/local/man/man /usr/local/man/cat
    /usr/pdkg/man/man  /usr/pdkg/man/cat
    /usr/XR11/man/man  /usr/XR11/man/cat
"
for prefix in $prefixes; do
  for n in 1 2 3 4 5 6 7 8; do
    path="${prefix}$n"
    # ...
  done
done
glenn jackman
+1  A: 

you can use find

find /usr/share /usr/local /usr/dpkg /usr/XR11 -type d \( -name "man[0-9]" -o -name "cat[0-9]" -o -name "cat" -o -name "man" \)
A: 

You can use find:

find /usr/share /usr/local /usr/dpkg /usr/XR11 -type d -regex '.*/man/\(cat\|man\)[1-8]$'

On my system, I have a bunch of localized man pages that would get excluded by the above, so you could do this:

find /usr/share /usr/local /usr/dpkg /usr/XR11 -type d -regex '.*/\(cat\|man\)[1-8]$'
Dennis Williamson