LinuxBasics.org

The community that helps people to run Linux

rss

xw.sh - The Crossword-Solver

This is how this script evolved: Basically, I just use the commands that I used on the commandline before.

For each error I encountered on the commandline (e.g. mydict not where I thought it was), I added an ‘if’ before the command that would fail. I did the conditions in a way that inside the if, I could execute the commands to ‘cure’ the error-condition, so after that, the command would run without error.

If I was too lazy to fix the error-condition in the script (or it would make no sense), I echo an error-message and ‘exit 1’. (See in the nested if in case ispell is not available on the system.)

When designing the script, we encountered more ‘special cases’ and can add the appropriate commands. Apostrophe and Umlaut handling would be two omissions from my script that simply need another ‘if’ and the command that are tested on the commandline.

That is why I prefer command-line tools to do things over GUI. GUI is nice, but commandline-applications (like imagemagick, tex and others) can be scripted and thus automated. My preference for the cli is documented here: Welcome to the real world

#! /bin/bash
# xw - a script to help find words for crossword puzzles
# V0.2 by Stefan Waidele jun.
# This is GPL-software :)
#
# ~/bin/wx.sh "pa...a.ent"
# or
# ~/bin/wx.sh "n....bor" american
# ~/bin/wx.sh "n....bour" british

#
# Anchor the searchterm to the beginning and end of line
# -> put ^ and $ around it
# (This will also take eliminate the need to 'count the characters')
#
export word="^"$1"$"

#
# Get the dictionary-file
#
if test "$2" = "" ; then
# No dictionary-file specified, let's use ~/.xw/mydict.default
    export dict=~/.xw/mydict.default
    if ! test -e $dict ; then
# ~/.xw/mydict.default not there...we need that
	echo "No default dictionary defined."
	echo "Use 'ln -s <default-dict> ~/.xw/mydict.default' to define it"
	exit 1
    fi
else
# Use the specified dictionary
    export dict=~/.xw/mydict.$2
fi


if ! test -e $dict ; then
# create the dictionary if neccessary
    if test -e /usr/lib/ispell/$2.hash ; then
	echo -n "generating ~/.xw/mydict.$2 ... "
	cat /usr/lib/ispell/$2.hash \
	    | strings \
	    | tr 'A-Z' 'a-z' \
	    | grep '^[a-z]' \
	    | sort -u >mydict.$2
	echo "done"
    else
# Fail if the ispell-hashfile is not found
	echo "/usr/lib/ispell/$2.hash not available on this system. Sorry"
	exit 1
    fi
fi

#
# Find the words in the dictionary
#
grep $word $dict	

Copyright (c) by the authors.
Prior to editing, authors agreed to license their contributions by the terms of the GPL.
See our licensing page for details.


Linux® is a registered trademark of Linus Torvalds.


 
  historical/2005-course/wx.txt · Last modified: 2008/07/20 19:08

LinuxBasics.org

Start Linux-Course Tutorials Linux Links Security Blog Forum E-mail List Search Online Chat

Site-Info

Help Get in Touch Making of LBo

Wiki-Control

Powered by

Linux Apache DokuWiki Mailman RUTE ht://Dig