#! /usr/bin/ksh # # Author: Mike Fleming mike@tauzero.co.uk # # Allows all non-zero-length files in a directory to be selected for # viewing using more. # # As well as a number, N for next, P for previous, and Q to quit, if # ! is input, a shell command can be executed - it can either be supplied # after ! (eg. !ls -l) or, if it is not supplied there, it will be # requested. # # Usage: # morefromdir [ -c ] [ -t ] [ -v ] dirname # # Options # # -c Searches in more are case sensitive # -t Display in chronological order # -v Use fileview to view file PGLEN=25 CASE=-i LSFLAG="" DIR=. VIEWER=more USAGE="Usage: morefromdir [ -c ] [ -t ] dirname\n\n \t-c\tSearches in more are case sensitive\n \t-t\tDisplay in chronological order\n \t-v\tUse fileview to view file\n" while getopts :ctv opt do case $opt in c) CASE="";; t) LSFLAG=-t;; v) VIEWER=fileview;; ?) echo "Unknown option $OPTARG" echo $USAGE return 1;; :) echo "No value supplied for option $OPTARG" echo $USAGE return 1;; *) echo $USAGE return 1;; esac done shift $((OPTIND - 1)) if [[ "$1" != "" ]]; then DIR=$1 fi typeset -i ptr=0 l=$(ls $LSFLAG $DIR 2>/dev/null) for a in $l do if [[ -s "$DIR/$a" && ! -d "$DIR/$a" ]]; then fnames[$ptr]=$a ptr=$(($ptr + 1)) fi done if [[ -z "${fnames[0]}" ]] then echo "No matching files found" return fi # page = current page (starts at 0) # index = screen array pointer for display (starts at 0) # line = display line number (starts at 1) # ptr = pointer to file to display typeset -i page=0 typeset -i index typeset -i line while [[ "$REPLY" != "Q" && "$REPLY" != "q" ]] do lastline=$((PGLEN - 1)) line=1 index=$((page * lastline)) while [[ -n "${fnames[$index]}" && $line -le $lastline ]] do if (( line < 10 )) then lno=" "$line else lno=$line fi echo "$lno" ${fnames[$index]} index=$((index + 1)) line=$((line + 1)) done FURTHER="" if [[ -n "${fnames[$index]}" ]] then FURTHER=$FURTHER" [N]ext page," fi if (( page > 0 )) then FURTHER=$FURTHER" [P]revious," fi read REPLY?"No. of file to view,$FURTHER [Q]uit, ! - shell cmd: " if [[ "$REPLY" == "" ]] then REPLY="X" fi if (( $(echo $REPLY | grep -c "^[0-9]*$") > 0 )) then if (( REPLY > 0 && REPLY <= lastline )) then ptr=$(((page * lastline) + (REPLY - 1))) if [[ -n "${fnames[$ptr]}" ]] then fn=$DIR/${fnames[$ptr]} suffix=$(expr "$fn" : '.*\.\([^./]*\)$') if [[ "$suffix" == "Z" ]] then uncompress -c $fn | $VIEWER $CASE else $VIEWER $CASE $fn fi fi fi elif (( $(echo $REPLY | grep -c "^!") > 0 )) then CMD=$(echo $REPLY | cut -c 2-) if [[ "$CMD" == "" ]] then read CMD?"Command to execute: " fi if [[ "$CMD" != "" ]] then eval $CMD fi else case $REPLY in P | p) if (( page > 0 )) then page=$((page - 1)) fi;; N | n) if [[ -n "${fnames[$index]}" ]] then page=$((page + 1)) fi;; esac fi done