#! /usr/bin/ksh # # Author: Mike Fleming mike@tauzero.co.uk # # Allows a list of files to be selected for viewing using more or # fileview # # The special literal "today" or "today-n" where n is a number can be # used to indicate file names containing today's date or a date n days # in the past. By default the form ccyymmdd will be used - the -y option # specifies that the short yymmdd form is to be used. The -s parameter # can be used to specify a date separator character. # # 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: # morefrom [ -a ] [ -c ] [ -s separator ] [ -v ] [ -y ] filelist # Options # # -a Show all files in filelist, including zero-length files # -c Case sensitive searches in more # -s Specify a date separator character # -v Use fileview to view file # -y Use short date (yymmdd) for "today-n" date form PGLEN=25 CASE=-i CHK="" YR=Y DS=Y YRFLAG="" DSFLAG="" VIEWER=more VCASE=-i USAGE="morefrom [ -a ] [ -c ] [ -s separator ] [ -v ] [ -y ] filelist\n\n \t-a\tShow all files in filelist, including zero-length files\n \t-c\tSearches in 'more' command are case sensitive\n \t-s\tSpecify a date separator character (default is no separator)\n \t-v\tUse fileview to view file\n \t-y\tUse short date (yymmdd) for 'today-n' date form\n" while getopts :acs:vy opt do case $opt in a) CHK="A";; c) CASE="" VCASE="";; s) DS=$OPTARG DSFLAG="-s $OPTARG";; v) VIEWER=fv VCASE="";; y) YR=y YRFLAG=-y;; ?) echo "Unknown option $OPTARG" echo $USAGE return 1;; :) echo "Missing value for option $OPTARG" echo $USAGE return 1;; *) echo $USAGE return;; esac done shift $((OPTIND - 1)) typeset -i ptr=0 if [[ "$1" == "" && "$CHK" != "A" ]] then echo $USAGE return fi while [[ -n "$1" ]] do if [[ "$(echo $1 | cut -c1-5)" == "today" ]] then if [[ "$1" == "today" ]] then l=$(LC_COLLATE=C ls *$(date +%$YR$DS%m$DS%d)* 2>/dev/null) else dd=$(echo $1 | cut -c7-) l=$(LC_COLLATE=C ls *$(prev_date $DSFLAG $YRFLAG $dd)* 2>/dev/null) fi elif [[ "$1" == "yesterday" ]] then l=$(LC_COLLATE=C ls *$(prev_date $DSFLAG $YRFLAG 1)* 2>/dev/null) else l=$(LC_COLLATE=C ls $1) 2>/dev/null fi for a in $l do if [[ -s "$a" || ( -f "$a" && "$CHK" == "A" ) ]] then if [[ ! -d "$a" ]] then fnames[$ptr]=$a ptr=$(($ptr + 1)) fi fi done shift 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 line=1 lastline=$((PGLEN - 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=${fnames[$ptr]} suffix=$(expr "$fn" : '.*\.\([^./]*\)$') if [[ "$suffix" == "Z" ]] then uncompress -c $fn | $VIEWER $VCASE else $VIEWER $VCASE $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