Created
February 10, 2022 15:07
-
-
Save viktordw/c9dd50d86caa5f6acbf046d82fe15859 to your computer and use it in GitHub Desktop.
Less pager
This file contains 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 | |
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