Tag Archives: fun

Bash for the word lover

It’s a WYSIWYG world. After all, we’re nearly in the future, which I define as 2019, the year Rick Deckard chases down replicants in the Blade Runner. Still no flying cars, which is disappointing. Even so, we have Steve Jobs, so the future coming, right?

GUI everything isn’t all that it could be. For many, many tasks, it is more expeditious to open a terminal and get a bash prompt. CLI. Character. Text. It could be green on black, or it would be a rainbow on white, but it is not different from a Televideo terminal, or a Teletype for that matter. As good as the Bourne Again Shell is, it is not graphical or fancy.

What it is is efficient. For a sharp mind and one given to efficiency, the terminal is power. Want to replace frick with frack in 800 HTML files?

find ~/web/project3 -name '*.php' | xargs perl -pi -e 's/frick/frack/g'

Bam. Done.

This is why I have 3 terminals open right now. One is connected to a server somewhere in Texas. I just fixed some text on a site with a command much like the one above.

But you already knew all that. You Googled and found this page, so you are already 1337 or whatnot. How about some word power on the command line?

Install some packages

This will install the packages we will use on an Ubuntu or Debian system. For other distributions, you will need to use your distributions package system.

To install on Ubuntu or Debian, just install the needed APT packages:

sudo aptitude -y install wordnet wamerican-large curl wget an

Definitions

This grabs a definition for a word from dict.org. For “unusual” for example:

curl --stderr /dev/null dict://dict.org/d:unusual | sed '/^[.,0-9].*$/d'

Which returns:

Unusual \Un*u"su*al\, a.
Not usual; uncommon; rare; as, an unusual season; a person of
unusual grace or erudition. -- {Un*u"su*al*ly}, adv. --
{Un*u"su*al*ness}, n.
[1913 Webster]

As you can see you are using curl to request a definition for “unusual”, then using sed to filter the results, to exclude extra stuff you don’t want. You could just enter “curl dict://dict.org/d:unusual” for the raw deal. Good on ya.

You can turn this into a script:

#! /bin/bash
# display definition of a word
#
curl --stderr /dev/null dict://dict.org/d:$1 | sed '/^[.,0-9].*$/d'

Save that in a file called “def” and run “chmod +x def” to make it executable. Then “def unusual” will return the same definition. You just created your own tool. You rock.

Wordnet

How about more power? Princeton has a project called Wordnet, which organizes nouns, verbs, adjectives and adverbs into set of “cognitive synonyms” and provides tools to use this data. With Wordnet, synonyms, antonyms and other lexical relations can be found for a given word.

To show a definition, (still using “unusual” as an example):

wn unusual -over

Here’s the output:

Overview of adj unusual

The adj unusual has 3 senses (first 3 from tagged texts)

1. (24) unusual -- (not usual or common or ordinary; "a scene of unusual beauty"; "a man of unusual ability"; "cruel and unusual punishment"; "an unusual meteorite")
2. (1) strange, unusual -- (being definitely out of the ordinary and unexpected; slightly odd or even a bit weird; "a strange exaltation that was indefinable"; "a strange fantastical mind"; "what a strange sense of humor she has")
3. (1) unusual -- (not commonly encountered; "two-career families are no longer unusual")

This uses the “-over” option. Some other options are:

-synsa adjective synonyms
-synsn noun synonyms
-synsr adverb synonyms
-antsa adjective antonymns
-antsn noun antonymns
-antsr adverb antonymns

Wordnet is extensive and there are many more options, run “man wn” for more.

Crossword help

This is simply a use of grep to pattern match words in a word list file.

Use a regular expression to find a word. In quotes, start your pattern with a “^” character and end with a “$” character. Use a period “.” for each unknown character.

grep '^.a...f.c.n...$' /usr/share/dict/words

Magnificent!

Rhyming

This uses the rhyme project, which provides a rhyming dictionary for the command line.

To get, build and install rhyme on your system:

sudo aptitude -y install build-essential libgdbm-dev libreadline-dev
cd ~
DIR="src" && [ -d "$DIR" ] || mkdir "$DIR"
cd src
wget http://softlayer.dl.sourceforge.net/project/rhyme/rhyme/0.9/rhyme-0.9.tar.gz
tar -xzf rhyme-0.9.tar.gz
cd rhyme-0.9
make
sudo make install

Holy smokes, you just built software! There is no stopping you. To find a rhyme, using “house” as an example:

rhyme house

House rhymes! Lots of them.

Anagrams

Anagrams are pretty much pure word fun. It is fun to see what an anagram of your name is.

Print single-word anagrams of “andrew”:

an -l 1 andrew

Just call me the wander warden.

Mix in some CLI fun on your server

This post is directed at Ubuntu and Debian server admins. As all work and no play makes Jack a dull boy, it is imperative that you immediately make your server more fun. If you do not get a little smile when you log into your server via SSH, then something is terribly wrong! Avoid dullness by all means.

Here I will show how to add and use figlet, fortune, cowsay and xmlstarlet to have big banners, random quotes’n’quips, talking cows and word of the day appear when using SSH to access your server.

FIGlet is a program for making large letters out of ordinary text.

FIGlet project: http://www.figlet.org/

Fortune is a simple program that displays a random message from a database of quotations.

Cowsay is a filter that takes text and displays a cow saying it.

Cowsay project page: http://www.nog.net/~tony/warez/cowsay.shtml

Cowsay article: http://linuxgazette.net/issue67/orr.html

XMLStarlet is a set of command line utilities (tools) which can be used to transform, query, validate, and edit XML documents and files using simple set of shell commands in similar way it is done for plain text files using UNIX grep, sed, awk, diff, patch, join, etc commands.

XMLSartlet project page: http://xmlstar.sourceforge.net/overview.php

Adding Universe and Multiverse Repository in Ubuntu

These packages are in the Universe and Multiverse repositories. If you need to add these repositories, just un-remark the pertinent lines in /etc/apt/sources.list and run aptitude update:

First, make a backup of the original /etc/apt/sources.list file.

sudo cp /etc/apt/sources.list /etc/apt/sources.list.original

Edit /etc/apt/sources.list:

sudo vi /etc/apt/sources.list

Un-remark the universe and multiverse lines (remove the leading # character) so the lines look something like this:

deb http://us.archive.ubuntu.com/ubuntu/ lucid universe
deb-src http://us.archive.ubuntu.com/ubuntu/ lucid universe
deb http://us.archive.ubuntu.com/ubuntu/ lucid-updates universe
deb-src http://us.archive.ubuntu.com/ubuntu/ lucid-updates universe
deb http://us.archive.ubuntu.com/ubuntu/ lucid multiverse
deb-src http://us.archive.ubuntu.com/ubuntu/ lucid multiverse
deb http://us.archive.ubuntu.com/ubuntu/ lucid-updates multiverse
deb-src http://us.archive.ubuntu.com/ubuntu/ lucid-updates multiverse

Finish by retrieving the updated package lists to your system with:

sudo aptitude update

By the way, to search for packages in the Ubuntu packages repositories, visit:


http://packages.ubuntu.com/

Figlet

Figlet creates character graphic block letter banners. Thus:

media2

becomes…

                       ___      ___ 
   ____ ___  ___  ____/ (_)___ |__ \
  / __ `__ \/ _ \/ __  / / __ `/_/ /
 / / / / / /  __/ /_/ / / /_/ / __/ 
/_/ /_/ /_/\___/\__,_/_/\__,_/____/ 

Install Figlet

On an Unbuntu/Debian system, install figlet like this:

sudo aptitude install figlet

The following will create the media2 banner, as above:

figlet -f slant media2

Modifying Message of the Day

Once you are satisfied with the output of figlet, you can have your character graphics banner appear whenever a user uses SSH to access the server, just modify the /etc/motd.tail file with:

sudo figlet -f slant media2 >>/etc/motd.tail

Bingo, whenever a user logs into the media2 server then will see your nifty banner! A little dullness have been bannished.

Installing xmlstarlet, cowsay and fortune

These are the commands we’ll be using to offer some fresh content on every login.

sudo aptitude install xmlstarlet cowsay fortune

Putting it all together

Modify the /etc/bash.bashrc file. This affects all users that use bash as their default shell.

fortune -a | cowsay -f $(ls /usr/share/cowsay/cows/ | shuf | head -n1)

echo -n "word of the day: "
/usr/bin/xmlstarlet sel --net -t -m "/rss/channel/item/description" -v "." "http://dictionary.reference.com/wordoftheday/wotd.rss"

The fortune command pops out a random quip, piped into cowsay, which is configured here with -f to use a random character graphic image. Then, word of the day is sourced from an RSS feed with xmlstarlet.

With everything, here is an example of what will appear when your SSH into your server:

Linux ijuki 2.6.32-x86_64-somewhere #1 SMP Sat Dec 5 16:55:26 UTC 2009 x86_64

                       ___      ___ 
   ____ ___  ___  ____/ (_)___ |__ \
  / __ `__ \/ _ \/ __  / / __ `/_/ /
 / / / / / /  __/ /_/ / / /_/ / __/ 
/_/ /_/ /_/\___/\__,_/_/\__,_/____/ 

Last login: Sun May  2 18:39:14 2010 from 00.000.000.000
 ________________________________________
/ Everything that you know is wrong, but \
\ you can be straightened out.           /
 ----------------------------------------
       \    ____
        \  /    \
          | ^__^ |
          | (oo) |______
          | (__) |      )\/\
           \____/|----w |
                ||     ||

	         Moofasa
word of the day: sesquipedalianism: given to using long words.