Hi!

$ cat randtype.sh
>
> lines=`wc -l /etc/passwd | tr -s ' ' | cut -d' ' -f2`
> lineno=1
> while [ $lineno -ne $lines ];
> do
>        lineno=`expr $lineno + 1`
>        line=`sed -n ${lineno}p /etc/passwd`
>        charsinline=`echo $line | wc -c | tr -s ' ' | cut -d' ' -f2`
>        charcnt=1
>        while [ $charcnt -ne $charsinline ];
>        do
>                chartoprint=`echo -n $line | cut -c${charcnt}`
>                echo -n "$chartoprint"
>                sleep 0.12
>                charcnt=`expr $charcnt + 1`
>        done
>        echo
> done
>
>
Way too complicated! Here's a simplified version of the script:

-----8<----- cut below ------8<--------------------------
#!/bin/bash
SIZE=$(stat -c "%s" $1)
for ((i=0; i<SIZE; i++))
do
    read -n1 CHAR
    test -z "$CHAR" && echo || echo -n "$CHAR"
    sleep 0.1
done < $1
------8<----- cut till above line ----8<----------------

Save as slowtype.sh and make it executable. Run as below:
  ./slowtype.sh /etc/passwd



> $ cat randtype.py
>
> import time
> import os
> import sys
>
> f=open("/etc/passwd")
> lines=f.readlines()
>
> sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
> for line in lines:
> # First indentation level
>        for char in line:
>        # indent all following lines with a tab
>                 time.sleep(0.02)
>                 #print "slept"
>                 sys.stdout.write(char)
>
>
The above code is completely non-pythonic (looks like a
C program written in python!). The following code must be
succinct and simpler enough:

------------ 8<------- cut below --------- 8<-----------------
#!/usr/bin/env python
import time
import sys

for char in sys.stdin.read():
    sys.stdout.write(char)
    sys.stdout.flush()
    time.sleep(0.1)
---------- 8<------- cut till above ------ 8<------------------

Save the above script as slowtype.py and make it executable.
Run the script as below:
   ./slowtype.py < /etc/passwd

The same code in ruby would be something like below (even
simpler versions could be possible):

--------- 8<-------- cut below ------- 8<----------------------
#!/usr/bin/env ruby
STDIN.read.each_char do |c|
    STDOUT.write c
    STDOUT.flush
    sleep 0.1
end
--------- 8<------ cut till above -------- 8<-------------------

Save the above script as slowtype.rb and make it executable.
Run the script as below:
    ./slowtype.rb < /etc/passwd


Cheers,
Chandrashekar.

-- 
Chandrashekar Babu.,
http://www.chandrashekar.info/
_______________________________________________
ILUGC Mailing List:
http://www.ae.iitm.ac.in/mailman/listinfo/ilugc

Reply via email to