Skip to content

Instantly share code, notes, and snippets.

@viktordw
Created February 10, 2022 15:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save viktordw/c9dd50d86caa5f6acbf046d82fe15859 to your computer and use it in GitHub Desktop.
Save viktordw/c9dd50d86caa5f6acbf046d82fe15859 to your computer and use it in GitHub Desktop.
Less pager
#!/bin/bash
text=$(curl -s 'https://baconipsum.com/api/?type=meat-and-filler')
pager() {
# if stdout is not to a TTY, copy directly w/o paging
[ -t 1 ] || { cat; return; }
if [ -n "$PAGER" ]; then
"$PAGER"
elif command -v less >/dev/null 2>&1; then
less
else
echo "WARNING: No pager found; falling back to cat" >&2
cat
fi
}
while getopts "l" option; do
case $option in
l)
list='true'
;;
\?)
echo "wrong option."
usage
exit 1
;;
h)
usage
exit 0
;;
esac
done
shift $(($OPTIND - 1))
if [ "$list" = 'true' ]; then
pager <<EOF
$text
EOF
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment