You are not logged in.

#1 2009-09-13 04:19:12

yitzle
Member
Registered: 2008-10-19
Posts: 18

View man pages based on package name!

I have a tendency after installing a package to view all the man pages I just installed.
Manually? Ugh. Write a script!!

This script takes one or more package names and gets a list of the man pages part of that package using pacman -Ql
If there is only one page, it is displayed. If there are more, it prompts you for each one.

#!/bin/bash

# Retrieve a list of man pages from pacman for a package
manpages () {
    # Query pacman
    pacman -Ql "$1" | \
    grep '/usr/share/man/.' | \
    while IFS=' ' read _ file ; do 
        # Strip the path to a num and man page name
        file="${file#/usr/share/man/man}"
        num="${file%%/*}" 
        file="${file#*/}"
        file="${file%%.*}"
        [[ $file ]] || continue 
        echo "$num $file" 
    done
}

(( $# == 0 )) && echo "Usage: "$(basename "$0")" package [more_packages...]"

for arg ; do
    # Get the list of man number and names
    IFS=$'\n' list=( $(manpages "$arg" 2>&1) )

    # Check if any found
    if ! [[ "$list" ]] ; then
        echo "$arg has no man pages."
        continue
    fi

    p='error: package * not found'
    if [[ "${list[0]}" == $p ]] ; then
        list="${list[0]}"
        echo "${list#error: }"
        continue
    fi

    # Only show prompt for more than one man page
    prompt=0
    if (( ${#list[@]} > 1 )) ; then 
        echo "$arg has ${#list[@]} man pages."
        prompt=1
    fi

    for pair in "${list[@]}" ; do
        IFS=' ' read num name <<< "$pair"
        if (( prompt )) ; then
            # If prompting, ask and allow user to selectively skip pages
            read -p "Show man $pair? [Y/n] " -n 1
            [[ $REPLY == [nN] ]] && { echo ; continue ; }
        fi
        man "$num" "$name"
    done
done

---
EDIT: Based on a comment by Daenyth, changed from fgrep to grep to modify the handling of the man directory.

Last edited by yitzle (2009-09-13 04:35:45)

Offline

#2 2009-09-14 17:43:32

atordo
Member
Registered: 2007-04-21
Posts: 147

Re: View man pages based on package name!

Thanks for the script. I also like to have a glimpse at what I'm installing and this is very useful. In fact, I'm discovering new things about packages that were already installed... smile

Offline

#3 2009-09-14 18:39:19

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: View man pages based on package name!

you may find this simpler or more complex depending on your perspective:

basename "$(pacman -Ql zenity | grep '/usr/share/man/.*gz$')" | awk -F '.' '{print $1, $2}' | while read file num; do echo "man $num $file"; done
man 1 zenity

use it if you'd like smile.

/edit: slightly different approach.

Last edited by brisbin33 (2009-09-14 18:41:34)

Offline

#4 2009-09-14 19:10:51

atordo
Member
Registered: 2007-04-21
Posts: 147

Re: View man pages based on package name!

brisbin33: More compact, but it doesn't seem to get the same results. For example, trying with imagemagick will just result in Image::Magick(3pm) man page, whereas with the first script will display all the 18 man pages in the package.

Offline

#5 2009-09-14 19:49:53

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: View man pages based on package name!

sorry i was a bit shortsighted in testing.  this version works, though a double awk is pretty awkward (pun!); the existing bashisms may be best.  i'll keep thinking on this... it's a very nice script as is.

> pacman -Ql imagemagick | awk -F '/' '/usr\/share\/man\/.*gz$/ {print $NF}' | awk -F '.' '{print $1, $2}' | while read file num; do echo "man $num $file"; done
man 1 ImageMagick
man 1 Magick++-config
man 1 Magick-config
man 1 MagickCore-config
man 1 MagickWand-config
man 1 Wand-config
man 1 animate
man 1 compare
man 1 composite
man 1 conjure
man 1 convert
man 1 display
man 1 identify
man 1 import
man 1 mogrify
man 1 montage
man 1 stream
man 3pm Image::Magick

/edit: fixed variables.  thanks yitzle, i need to remember when i change variables b/w copy and paste i should change them both smile

Last edited by brisbin33 (2009-09-15 13:29:10)

Offline

#6 2009-09-15 02:13:40

yitzle
Member
Registered: 2008-10-19
Posts: 18

Re: View man pages based on package name!

brisbin33 wrote:

sorry i was a bit shortsighted in testing.  this version works, though a double awk is pretty awkward (pun!); the existing bashisms may be best.  i'll keep thinking on this... it's a very nice script as is.

> pacman -Ql imagemagick | awk -F '/' '/usr\/share\/man\/.*gz$/ {print $NF}' | awk -F '.' '{print $1, $2}' | while read man file; do echo "man $num $file"; done
man 1 ImageMagick
...

That last bit should be:

while read num file

Last edited by yitzle (2009-09-15 02:13:56)

Offline

Board footer

Powered by FluxBB