Skip to content

Instantly share code, notes, and snippets.

@for2ando
Last active October 16, 2022 13:11
Show Gist options
  • Save for2ando/c052d63b3d0870935f022c0dc556b71b to your computer and use it in GitHub Desktop.
Save for2ando/c052d63b3d0870935f022c0dc556b71b to your computer and use it in GitHub Desktop.
output files using $PAGER if the number of lines is grater or equal than $LINES, or output using cat.
#!/bin/bash
pname=$(basename "$0")
while true; do
case "$1" in
-l) lines="$2"; shift 2;;
-*) echo "$pname: '$1': no such option.">&2; shift;;
*) break;;
esac
done
test -z "$lines" && lines=$(tput lines)
test $# -le 0 && set -- -
buffer=$(mktemp -q -t "$pname.XXXXXXXX")
trap 'rm -r "$buffer"' 1 2 3 15 EXIT
cat "$@" >"$buffer"
if [ $(cat "$buffer" | wc -l) -lt $lines ]; then
cat "$buffer"
else
cat "$buffer" | ${PAGER:-less}
fi
## Makefile
XPAGER = $(shell which pageless || echo $${PAGER:-less})
output:
cat Contents.txt | $(XPAGER)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment