Last active
May 20, 2022 22:36
-
-
Save sitemapxml/c92e2c27e641909d1fad759bb4493b29 to your computer and use it in GitHub Desktop.
Pager example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Function that picks the best available pager | |
pager() { | |
# if stdout is not to a TTY, copy directly w/o paging | |
[ -t 1 ] || { cat; return; } | |
if [ -n "$PAGER" ]; then ## honor the user's choice, if they have a pager configured | |
"$PAGER" | |
elif command -v less >/dev/null 2>&1; then | |
less | |
elif command -v more >/dev/null 2>&1; then | |
more | |
else | |
echo "WARNING: No pager found; falling back to cat" >&2 | |
cat | |
fi | |
} | |
# Alternative function | |
# pager() { | |
# [ -t 1 ] || { cat; return; } | |
# less | |
# } | |
usage () { | |
echo 'USAGE: ' | |
echo ' pager.sh -l ' | |
} | |
while getopts "lh" option; do | |
case $option in | |
l) | |
list='true' | |
;; | |
h) | |
usage | |
exit 0 | |
;; | |
*) | |
usage | |
exit 0 | |
;; | |
esac | |
done | |
shift $(($OPTIND - 1)) | |
if [ "$list" = 'true' ]; then | |
pager <<'EOF' | |
here is our documentation | |
several pages of content here | |
etc, etc, etc | |
EOF | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment