A script to generate random text via jot

Jun 11, '08 07:30:00AM

Contributed by: benholt

There's already one hint on this site about the many wonders of the Unix utility jot. However, I was bored and decided to come up with some fun jot applications. So after hours of tweaking and coding, I came up with a script that lets you pick between three different types of output:

  1. Monkey typing random words.
  2. Geeky-looking code spam.
  3. Random English words from the dictionary.
Choose one, and your terminal window will then fill with all the randomness you could ever want; type Control-C to stop the program. Possible applications include looking busy at work, lorem ipsum for filler text, and generally feeling geeky.

My favorite is the code spam option. My best friend's fiancee took one look at my screen and said in an awed voice, "What is that?," in the same tone that one might say, "You built a nuclear reactor in your back yard!?" So I suppose all that time coding was worth it :). Read on for the script and a bit of detail on how it works.

Here's the script:

#!/bin/bash

#Ben Holt's fun jot script!
#http://theholtsite.com
#ben @ theholtsite.com

#Help from http://unixjunkie.blogspot.com/2007/10/generating-random-words.html

line=150
n=`cat /usr/share/dict/words | wc -l`

clear; echo; echo "I'm a code monkey! I can type all day! To stop me, press Control-C."; echo
echo "What should I type?"; echo

echo "1) Shakespeare/Lorem Ipsum"
echo "2) Spew Code!"
echo "3) Random English Words"
echo
echo ""

read arg

clear

case $arg in
    1)
    #Word spam
    while [ 1 ]; do
        nice jot -r -n -c $line a z | rs -g | sed -e "s/..\{`jot -r 1 3 6`\}/ &/g" | sed -e "s/..\{`jot -r 1 3 6`\}/ &/g" | sed -e "s/[ ]\{2,\}/ /g" | tr -d "[:cntrl:]"
        sleep .2
    done
;;
    2)    
    #Code spam
    while [ 1 ]; do
        nice jot -r -s "" -c `jot -r 1 1 $line` | sed -e "s/.\{`jot -r 1 4 6`\}/ &/g" | sed -e "s/.\{`jot -r 1 4 6`\}/ &/g" | tr -d "[:cntrl:]" | sed -e "s/.\{`jot -r 1 1 $line`\}//g" | tr "[:upper:]" "[:lower:]"
        sleep .2
    done
;;
    3)
    while [ 1 ]; do
    #Random English
        #perl -nle '$word = $_ if rand($.) < 1; END { print $word }' /usr/share/dict/words | tr "[:cntrl:]" " "
        cat /usr/share/dict/words | head -`jot -r 1 1 $n` | tail -1 | tr '\012' " "
        sleep .1
    done
;;
*)
    echo "Not a valid command."
    exit
esac
    
done
The line that makes it tick is this one:
nice jot -r -s "" -c `jot -r 1 1 $line` | sed -e "s/.\{`jot -r 1 4 6`\}/ &/g" | sed -e "s/.\{`jot -r 1 4 6`\}/ &/g" | tr -d "[:cntrl:]" | sed -e "s/.\{`jot -r 1 1 $line`\}//g" | tr "[:upper:]" "[:lower:]"
The script makes extensive use of jot, sed, and tr. There may be a better way to do this (there usually is). There's also many different ways to tweak it, such as changing the $line variable and the jot values of 4 and 6 (which affect spacing). Note that all control characters are removed so that nothing will ever be executed (this is a good thing).

[robg adds: This script works as described -- just remember to make it executable (chmod a+x script_name), and soon, your Terminal window can be filled with a bizarre assortment of words, gibberish, or geeky-looking code. Why one might want to run this script regularly is left as an exercise for the reader :).]

Comments (9)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20080611052740969