Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

jamesapp

macrumors 6502a
Original poster
Mar 7, 2008
544
0
got the following from a unix website. just wondering how i could get my computer to scroll. like in terminal, using ls or cat, can i have the text slowly scroll? when they talk about scripts, what are they reffering to? i tried pasting one of the scripts listed below into my applescript editor, but got error messages when i tried to run the script. i also tried to import one of the scripts into my python IDLE, and got error messages. when they talk about a script what language is this? i also tried to paste the c program into a file. i compiled the program in terminal, it compiled without any errors. but when i went to run it nothing happened. can someone help me try to get my macbook pro to scroll?

Is there a way scroll text instead of page?
Is there a way to slowly scroll the output of a file instead of page or cat ?

Instead of one page at a time, I would like to slowly scroll the displayed output of the file.

Well, instead of using more, you can use less, if it's installed on your box.
You could also use more, and use the "j" and "k" keys to scroll line-by-line up and down.

Hope that helps.


more won't work for me.
I want the screen to scroll through the text automatically without keyboard intervention for the duration of the display. I didn't know if it was possible to do that or not.

Perhaps a command line filter using tail -f ?

Could this work for you?


How would a filter be done with tail?
How would I do that?

Just another brainstorming idea but, would I be able to read a file only say 5 lines at a time, and then sleep for 1 second before reading the next 5 lines? That may be an option but I am unsure How I would do that. Suggestions anyone?

Thanks,


Well, five at a time, then sleep for one second was a little too fast for me, but feel free to change it to what you need:

Code:
#!/bin/sh
if [ "$#" -ne "1" ]; then
echo
echo "Usage: `basename $0` /path/to/text/file"
echo
exit 1
fi
if [ ! -f "$1" ]; then
echo
echo "File does not exist!"
echo
exit 2
fi

Line_total=`wc -l $1 | awk '{print $1}'`
Line_end=5

##
## See the next post for a fix to this
## next piece. Important!
##
until [ "$Line_end" -ge "$Line_total" ];
do
head -n${Line_end} $1 | tail -n5
Line_end=`expr ${Line_end} + 5`
sleep 4
done

That's just a quickie... It could be made much better (with command-line options and everything) if you want to put some time into it... It should be portable. Please let me know if it doesn't work for some reason.

Oops
Oops, I'm a bonehead...

The line:
until [ "$Line_end" -ge "$Line_total" ];
should have a "-gt" in the middle, not a "-ge"... It could cause you not to get the full results of the file...

Also, head may (depending on your system) have a hard-limit set. You may not be able to use this script with files over a certain length (I think it's 500 lines on some systems)...

Still, hope it helps...


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



Thanks Perderabo -
This script is a little over my head but I will test it out. Thanks.


This scroller is exactly what i needed


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.



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);
}

wish I knew c
Hey that's cool.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.