Visit Our UNIX and Linux User Community


Scrolling text continued


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Scrolling text continued
# 1  
Old 07-29-2009
Scrolling text continued

Perderabo *
Unix Daemon
*

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,071
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

---------- Post updated at 01:47 PM ---------- Previous update was at 01:28 PM ----------

I found this thread awhile back but it was closed parebado I think that is how you spell it wrote the code. I have scroller on my computer but when I type
scroller filename
I get an error message: something along the lines no such file or directory
just wondering what I am doing wrong I remember it working? I believe it has something to do with it being a shell script? Any help on getting it working would be appreciated. Also I see on movies and on my old ms-dos computer where the cursor moves like from left to right and text just appears as the cursor moves from what I remember the cursor on this program stays in the bottom left of the screen, just looking to see if someone knew of a way to have the cursor move and have text scroll? From what I remember this program reads like 5 lines and then sleeps for a second and then reads another 5 lines, which I think is nice. I think it would be nice to be able to toggle this program on and off like if you toggle it on output would scroll if you toggles it off output would be the default unix output, also it would be nice to be able to stop text from scrolling at the end of a page, like so you could read one page and it would stop. And then if you hit another key it would resume scrolling the next page. I don't know if any of this is possible and I don't have a lot of experience in programming, but it is something I have always thought about. Any help?
 

Previous Thread | Next Thread
Test Your Knowledge in Computers #582
Difficulty: Medium
A buffered input or output stream is one in which data that is read or written is actually stored in memory in a buffer until such a time as the buffer is flushed and the accumulated data is passed to or from the actual file.
True or False?

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Reading a long literal continued next line

I am trying to identify all messages or prompts from a number of COBOL programs and they can usually be identified by a pair of double quotes on one line. However, sometimes the literal will not be finished on the first line but after a dash in column 7 of the next line, the literal will be... (6 Replies)
Discussion started by: wbport
6 Replies

2. Shell Programming and Scripting

Parallel processing - continued

Hi, I am taking up the cue from where I was left in my earlier post ( link given below ) https://www.unix.com/shell-programming-scripting/231107-implement-parallel-processing.html I actually wanted to know the significance of using the Unix "wait" , which returns the control from background to... (3 Replies)
Discussion started by: kumarjt
3 Replies

3. Shell Programming and Scripting

Scrolling through text while interacting with program prompts

Hi all, I am trying write a shell script to automate the installation of a program, but during the process of the installation, the installation program requires the user to scroll through 10 pages of a license agreement. Since this is coming from stdout and is not a prompt, I am unable to send... (4 Replies)
Discussion started by: nanlee
4 Replies

4. Shell Programming and Scripting

Error in script confirmed and to be continued

Hello. I have a problem. When i run 1.sh, ask me yes or no, and, if i asnwerd yes, there continue the procediment "sudo -u root echo hello". All without putting it into a single script, and who uses the confirm many more scripts. I have two script. ============ 1 with the following... (2 Replies)
Discussion started by: ave-phoenix
2 Replies

5. Shell Programming and Scripting

scrolling cursor

Hi, I'm writing scripts in perl and shell and want to add the oprion of scrolling cursor on the screen when there is no output to the screen for long time. I saw it in some script but I don't have the source code. Are anyone know how can I perform this ? Thanks (1 Reply)
Discussion started by: Alalush
1 Replies

6. AIX

vi scrolling issue

Hello, I am using poderosa to open a terminal in an aix box. The problem is when i use a sudo, the vi scroll doesnt work properly, It will scroll through the first page, but when it reaches the end, only the last line scrolls, not the whole screen, making it hard to read and work. Its like... (1 Reply)
Discussion started by: jinxor
1 Replies

7. Shell Programming and Scripting

Vi horizontal scrolling

I have to look through logfiles where lines are several hundred characters long and if I open the log in Vi it automatically word wraps the line. In Vim you can use the -nowrap option to stop this, but how can you do this in Vi? I ask because I don't want to see the whole line, just the first few... (8 Replies)
Discussion started by: rupweb
8 Replies

Featured Tech Videos