new-words

view learn-words.sh @ 6:d1851ee29a1a

minifix
author Igor Chubin <igor@chub.in>
date Wed Mar 10 10:38:27 2010 +0200 (2010-03-10)
parents
children
line source
1 #!/bin/bash
3 LANGUAGE=en
4 N=50
5 REVERSED=NO
7 show_usage()
8 {
9 cat<<USAGE > /dev/stderr
11 Usage:
12 $0 [ -l LANG ] [ -r ] [ N ]
14 Help to learn N (number) previously marked words from LANG language.
16 Options:
17 -r reversed mode (show the note first; the word second)
19 By default:
21 LANG = $LANGUAGE
22 N = $N
23 REVERSED = $REVERSED
25 USAGE
26 }
28 if [ "$1" = -l ]
29 then
30 if [ -n "$2" ]
31 then
32 LANGUAGE="$2"
33 shift 2
34 else
35 echo "Error: Please, specify LANGUAGE (en, de, ru,...) after -l or don't use -l switch" > /dev/stderr
36 show_usage
37 exit 1
38 fi
39 fi
41 if [ "$1" = -r ]
42 then
43 REVERSED="YES"
44 shift
45 fi
47 if [ -n "$1" ]
48 then
49 if echo "$1" | grep -qx '[0-9]\+'
50 then
51 N="$1"
52 else
53 echo "Error: Number of words N should be numeric. " > /dev/stderr
54 echo " Please provide a number or don't specify N at all " > /dev/stderr
55 show_usage
56 exit 1
57 fi
58 fi
60 notes_file=~/".new-words/notes-$LANGUAGE".txt
61 if [ -e $notes_file ]
62 then
63 true
64 number="`wc -l $notes_file | awk '{print $1}'`"
65 if [ "$number" -lt "$N" ]
66 then
67 echo "Warning: The notes file for the language is too short. Truncating N to $number" > /dev/stderr
68 N="$number"
69 fi
70 while true
71 do
72 main_script="$$"
73 tail -n $((1+$RANDOM%$N)) $notes_file | head -1 | while read word note
74 do
75 trap "kill -1 $main_script; kill -1 $$" 2
76 if [ "$REVERSED" = "NO" ]
77 then
78 echo $word
79 read < /dev/tty
80 echo $note
81 read < /dev/tty
82 clear
83 else
84 echo $note
85 read < /dev/tty
86 echo $word
87 read < /dev/tty
88 clear
89 fi
90 done
91 done
92 else
93 echo "Error: Uknown language '$LANGUAGE'" > /dev/stderr
94 echo " File $notes_file not found" > /dev/stderr
95 exit 1
96 fi