#!/bin/busybox ash # (c) Robert Shingledecker 2009 # $1 is title, $2 is file to read from, $3 is offset 0 or 1 (1 default) # . /etc/init.d/tc-functions useBusybox # [ -z "$2" ] && exit 1 FILEIN="$2" [ "$FILEIN" == "-" ] && FILEIN=/dev/stdin OFFSET=1 [ -n "$3" ] && OFFSET="$3" awk -v title="$1" -v offset="$OFFSET" -v answer="/tmp/select.ans" -v r="$(expr `stty size | cut -d" " -f1` - 2)" ' function read_console() { "head -1 < /dev/tty" | getline results close("head -1 < /dev/tty") return results } { A[NR] = $0 } END { if ( NR < 1 ) { print "q" > answer exit } if ( NR == 1 ) { print A[1] > answer exit } do { system ("clear") printf "%s\n", title for (l=1; l<=r ; l++) { ++j if ( j <= NR ) printf "\t%2d. %s\n", j, A[j] } printf "Enter selection ( 1 - %s ) or (q)uit", NR if ( NR > r ) printf ", (n)ext, (p)revious: " printf ": " selection = read_console() if (selection == "q") break if (selection == "p") { if ( j > r ) j = j - r * 2 else j = 0 continue } if (selection == "n" || selection == "") { if ( j > NR ) j = j - r continue } selection = selection + 0 if (selection+0 < 1 || selection+0 > NR ) j = j - r } while (selection < 1 || selection > NR) if ( offset == 1 && selection != "q") print A[selection] > answer else print selection > answer } ' < "$FILEIN"