Skip to content

Instantly share code, notes, and snippets.

@Chiogros
Last active January 29, 2023 16:51
Show Gist options
  • Save Chiogros/3f04c7a913bfb6171ceb71192c98aa56 to your computer and use it in GitHub Desktop.
Save Chiogros/3f04c7a913bfb6171ceb71192c98aa56 to your computer and use it in GitHub Desktop.
Print a random manpage. You can specify a section to pick the manpage from.
#!/bin/bash
# Print a random manpage.
# You can specify a section to pick manpage from.
NROFF_DIR=/usr/share/man/man
PAGE_EXT=gz
MANPAGES_LIST=/tmp/manpages
section="*"
# Get manpage section wanted
if [ $# -eq 1 ]
then
if [ "$1" == "-h" ] || [ "$1" == "--help" ]
then
echo "usage: $0 [section]"
echo ""
echo "section: 0-9, show only a manpage of this section"
exit 0
elif [ "$1" -ge 0 ] && [ "$1" -le 9 ]
then
section="$1"
fi
fi
# List all manpages (of specified section)
find "${NROFF_DIR}"${section} -name "*${PAGE_EXT}" 1> ${MANPAGES_LIST}
lines=$(wc -l ${MANPAGES_LIST} | cut -d ' ' -f 1)
if [ "${lines}" -eq 0 ]
then
echo "No manpage in this section..." >&2
exit 1
fi
# Pick a random manpage
page=$(sed -n "$((RANDOM % lines))p" ${MANPAGES_LIST})
/bin/man "${page}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment