The UNIX and Linux Forums  

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 01:22 AM
lpr- how to print from page to page naamas03 Shell Programming and Scripting 4 12-26-2007 03:30 AM
scroll bars in Exceed X-windows yankee428 UNIX for Dummies Questions & Answers 7 08-18-2005 05:17 AM
linking unix generated text file to html page alexd Shell Programming and Scripting 1 11-13-2002 09:21 AM
Text Modification and page I/O error jyotipg High Level Programming 4 10-05-2001 06:43 AM

Closed Thread
 
Submit Tools LinkBack Thread Tools Display Modes
  #8  
Old 12-18-2001
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,618
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 10:52 AM.
Forum Sponsor
  #9  
Old 12-21-2001
Registered User
 

Join Date: Dec 2001
Location: Overland Patrk
Posts: 62
Thanks Perderabo -

This script is a little over my head but I will test it out. Thanks.
  #10  
Old 12-21-2001
Registered User
 

Join Date: Dec 2001
Location: Overland Patrk
Posts: 62
This scroller is exactly what i needed

Perderabo,

This script is perfect. Do you know if there is a way to sleep for less than 1 second? I couldn't find any doc on whether that was possible or not. I think this would make it scroll smoother.
  #11  
Old 12-22-2001
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,618
Sleeping for less than a second is not a real good idea in a script. The overhead required to launch a process is too great. The right thing would be to switch to c. But it is easy to write a program like sleep that will sleep for less than a second if you want to give it a try. I wrote one that I call "nodoff". It sleeps for milliseconds. So "nodoff 500" is a half a second. Here it is...
Code:
#ifdef __STDC__
#define PROTOTYPICAL
#endif
#ifdef __cplusplus
#define PROTOTYPICAL
#endif

#include <stdlib.h>
#include <sys/time.h>

#ifdef PROTOTYPICAL
int main(int argc, char *argv[])
#else
main(argc,argv)
char *argv[];
#endif

{
        int millisecs, microsecs;
        struct timeval tv;

        millisecs=atoi(argv[1]);
        microsecs=millisecs*1000;

        tv.tv_sec=microsecs/1000000;
        tv.tv_usec=microsecs%1000000;

        select(0, NULL, NULL, NULL, &tv);
        exit(0);
}

Last edited by Perderabo; 01-16-2005 at 12:17 PM. Reason: Remove html which is no longer supported
  #12  
Old 12-28-2001
Registered User
 

Join Date: Dec 2001
Location: Overland Patrk
Posts: 62
wish I knew c

Hey that's cool.
It looks like me learning c would be a good thing. Any suggestions on a good place to start for a beginner?
  #13  
Old 01-03-2002
Registered User
 

Join Date: Jan 2002
Location: Bandung, Indonesia
Posts: 11
use less (http://unixhelp.ed.ac.uk/CGI/man-cgi?less), or more (http://unixhelp.ed.ac.uk/CGI/man-cgi?more)
__________________




-- negative
Google The UNIX and Linux Forums
Closed Thread

Thread Tools
Display Modes




All times are GMT -7. The time now is 12:11 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0