6
\$\begingroup\$

This is update to a previously asked question Bash manual page selection menu. This script generates a menu for the user to select which manual page he/she would like to read.

#!/bin/bash

# Mac OS X specific function to clear completely clear the screen.
# Seems to work well for the purposes of this script 
clear(){    
    osascript -e \
    'set theApp to (get the path to the frontmost application) as text
    set this_app to the name of application theApp
    activate application this_app
    tell application "System Events" to keystroke "k" using command down'   
}
clear
COLUMNS=80
while true
do
    printf 'Man Pages: main menu\n¯¯¯¯¯¯¯¯¯\n'
    PS3=$'\nMake a selection: '
    select dir in $(ls -d ${PATH//:/ })
    do
        case $REPLY in
            [0-9]*) ls $dir; break 1;;
            *) echo "Exiting" && exit 1;;
        esac
    done
    clear
    printf "Man Pages: $dir"$'\n¯¯¯¯¯¯¯¯¯\n'    
    PS3=$'\n(B)ack to main menu\n(Q)uit\n\nMake your selection: '
    while true
    do
        select file in $(ls ${dir} | column -t)
        do
            case $REPLY in
                [0-9]*) man $file; clear; break 1;;
                [bB]) clear; break 2;;
                [qQ]) echo "Exiting" && exit;;
                   *) echo "Exiting" && exit 1;;
            esac
        done
    done
done

I wanted to make it so that any programs without a manual page wouldn't be listed in the menu. But to do so would require checking to see if every command found in the user's PATH had a manual page associated with it and then creating a database of commands with manual pages. As it is, if a command does not contain a manual page, the user is directed back to the previous menu page to choose another selection.

I realize there is already a clear command in most user's terminals. But the clear command in my system clears the screen but still allows the user to scroll up past where the screen was just cleared. I made the clear function to completely clear the screen to enhance the overall aesthetic when running the script. It's not really necessary but I think it looks nice.

I think this script does what I intended it to do. So now I'd like other's opinions on it. Is there anything I could do to improve it?

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

I guess the reset command to clear terminal and scrollback buffer isn't available on your target? In any case, that's an irritating thing for a program to do.


Here, we're substituting the output of ls:

     select dir in $(ls -d ${PATH//:/ })

Although it's unusual for PATH to contain names with spaces, that's certainly a possibility. Better to turn it into an array:

    readarray -d : -t pathdirs <<<"$PATH"
    select dir in "${pathdirs[@]}"

Instead of listing all the files (executable or not!) in the program search path, consider instead listing all the manual pages in the manual search path. On my system, that would be $(manpath); perhaps we might filter to just those pages in section 1 ("user commands").


It's usually a bad idea to interpolate variables into a printf format string, like this:

    printf "Man Pages: $dir"$'\n¯¯¯¯¯¯¯¯¯\n'

We could run echo instead, but perhaps better to use %s format specifier:

printf 'Man Pages: %s\n-----------%.*s\n' "$dir" \
       ${#dir} '----------------------------------------------------'

Then it's fine if $dir contains % or \ (and it gives us the correct length of underline).

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.