-
-
Save wd5gnr/797189d8fac7f8b3699a95d120df085c to your computer and use it in GitHub Desktop.
Dumb more/less command if the system doesn't give you one and you don't want to install
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/ash | |
| # Default lines per page | |
| LINES_PER_PAGE=${PAGER_LINES:-20} | |
| # Function to display a page | |
| display_page() { | |
| local start_line=$1 | |
| local end_line=$2 | |
| while [ $CURRENT_LINE -le $END_LINE ]; do | |
| if IFS= read -r aline ; then | |
| echo "$aline" | |
| CURRENT_LINE=$(( CURRENT_LINE + 1 )) | |
| else | |
| EOF=1 | |
| break | |
| fi | |
| done | |
| } | |
| # Check if a file is provided | |
| DONE=0 | |
| FN=stdin | |
| while [ $DONE -eq 0 ] ; do | |
| if [ ! -z "$1" ]; then | |
| exec 0< "$1" | |
| FN="$1" | |
| shift | |
| fi | |
| CURRENT_LINE=0 | |
| EOF=0 | |
| if [ -z "$1" ]; then | |
| DONE=1 # done when this is over? | |
| fi | |
| while [ $EOF == 0 ] ; do | |
| END_LINE=$(( CURRENT_LINE + LINES_PER_PAGE - 1 )) | |
| clear # Clear screen for a cleaner page display | |
| display_page "$CURRENT_LINE" "$END_LINE" | |
| if [ $EOF == 0 -o $DONE == 0 ]; then | |
| echo -e "\n$FN -- Press Enter for next page, 'q' to quit 'n' for next file --" | |
| read -r -n 1 USER_INPUT </dev/tty | |
| if [[ "$USER_INPUT" == "n" || "$USER_INPUT" == "N" ]]; then | |
| EOF=1 | |
| break; | |
| fi | |
| if [[ "$USER_INPUT" == "q" || "$USER_INPUT" == "Q" ]]; then | |
| EOF=1 | |
| DONE=1 # bail | |
| break | |
| fi | |
| fi | |
| CURRENT_LINE=$((END_LINE + 1)) | |
| done | |
| if [ ! -z "$1" ]; then | |
| true | |
| else | |
| break | |
| fi | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment