The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Seeing the screen output beyond the scroll capability for the last run command bimukt UNIX for Dummies Questions & Answers 1 05-02-2008 04:22 AM
lpr- how to print from page to page naamas03 Shell Programming and Scripting 4 12-26-2007 06:30 AM
scroll bars in Exceed X-windows yankee428 UNIX for Dummies Questions & Answers 7 08-18-2005 08:17 AM
linking unix generated text file to html page alexd Shell Programming and Scripting 1 11-13-2002 12:21 PM
Text Modification and page I/O error jyotipg High Level Programming 4 10-05-2001 09:43 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-18-2001
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,119
Using head and tail like that is terribly inefficient. I decided to try a rewrite. Sheesh...I spent all morning on this....
Code:
#! /usr/bin/ksh
#
#  scroller ---  display text, but sleep every few lines.
#
#
#  scroller will copy 5 lines from standard input to the 
#  local terminal and then sleep for one second.  
#
#  You  can override the 5 lines with a "-l n" option.  
#  And you use  a "-s n" to override the sleep time.
#
#  You can type your interrupt character (often control c)
#  to get a menu that will let you adjust these values.
#
#  You can use -m to go straight to the menu at startup
#  
#  You can specify filenames or scroller can just read from 
#  standard in.
#
USAGE="usage: scroller [-s n] [-l n] [-m] [filename1] [filename2] ..."
  
#
#  initialize variables
#
#  Set reasonable initial values here.  TASK controls the main 
#  loop as described below.  snooze is the number of seconds for
#  the sleep.  lpp (lines per page) is the number of lines to
#  dispaly before sleeping.  lines will repeatedly cycle from 0
#  to npp as the program runs.  totallines is a count of lines
#  processed so far.  And we turn off IFS so the reads work with
#  data that has leading white space.
  
TASK=2
snooze=1
lpp=5
lines=0
totallines=0
IFS=""

#
#  Now Process the command line
#

error=0
while getopts :s:l:m  o ; do
        case $o in
        s)      if [[ $OPTARG -gt 0 && $OPTARG -lt 100 ]] ; then
                        snooze=$OPTARG
                else
                        print $OPTARG is illegal
                        error=1
                fi
                ;;
        l)      if [[ $OPTARG -gt 0 && $OPTARG -lt 100 ]] ; then
                        lpp=$OPTARG
                else
                        print $OPTARG is illegal
                        error=1
                fi
                ;;
        m)      TASK=2
                ;;
        ?)      print error argument $OPTARG is illegal
                error=1
                ;;
        esac
done
shift OPTIND-1
if ((error)) ; then
        print $USAGE
        TASK=0
fi


#
#  Open first file or indicate that we are using stdin
#

if (($# >= 1)) ; then
        input=$1
        exec < $input
        shift
else
        input="(standard input)"
fi


#
#  Major loop.  TASK can be 0 or 1 or 2.  If TASK is set we 
#  loop doing either task 1 or task 2.  Either task can switch 
#  to the other.  And both tasks can abort by setting TASK=0.  
#  But control c will switch to task 1.
#

trap interrupt=1 2
interrupt=0
while ((TASK)) ; do
        if ((interrupt)) ; then
                interrupt=0
                TASK=1
        fi
        case $TASK in

#
#   Task 1 --- display service menu
#

        1)      print  
                print  '=====[[ scroller service menu ]]====='
                print
                print  "      $totallines lines processed so far from $input"
                print  "      sleep seconds = $snooze"
                print  "      lines per page = $lpp"
                print  
                print  '   1) list the text'
                print  '   2) set lines per page'
                print  '   3) set sleep seconds'
                print  '   4) abort'
                print
                print -n  "[Enter Selection]====>>"
                read < /dev/tty
                case $REPLY in 
                1)      TASK=2
                        ;;
                2)      print lines per page is currently $lpp
                        print -n enter new value --
                        read val < /dev/tty
                        if [[ $val -gt 0 && $val -lt 100 ]] ; then
                                lpp=$val
                        else
                                print $val is illegal
                        fi
                        ;;
                3)      print sleep seconds is currently $snooze
                        print -n  enter new value --
                        read val < /dev/tty
                        if [[ $val -gt 0 && $val -lt 100 ]] ; then
                                snooze=$val
                        else
                                print $val is illegal
                        fi
                        ;;
                4)      TASK=0
                        ;;
                *)      print illegal response
                        print REPLY = $REPLY
                        ;;
                esac
                ;;

#
#  Task 2 --- copy lines from stdin to stdout
#
#    Read a line, increment counts, sleep if it's time
#

        2)      if read line ; then
                        print -r -- "$line"
                        ((totallines=totallines+1))
                        ((lines=lines+1))
                        if ((lines >= lpp)) ; then
                                lines=0
                                sleep $snooze
                        fi
                else
#
#    No more input.  Open a new file or give up.
#

                        if [[ $# -ge 1 ]] ; then
                                input=$1
                                exec < $input
                                shift
                        else
                                TASK=0
                        fi
                fi
                ;;

#
#   Task * --- can't happen
#
        *)      print -u2 $0: impossible error TASK = $TASK
                TASK=0
                ;;
        esac
done

((totallines)) && print "$totallines" total lines processed

exit 0

Last edited by Perderabo; 12-18-2001 at 01:52 PM..
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:12 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0