Browse Source

Define locals properly with docstrings

Alois Mahdal 3 years ago
parent
commit
bfa829befb
1 changed files with 52 additions and 34 deletions
  1. 52
    34
      99bottles.sh

+ 52
- 34
99bottles.sh View File

20
     #
20
     #
21
     # Get predecessor to a number $1
21
     # Get predecessor to a number $1
22
     #
22
     #
23
-    case $1 in
24
-        *nine)      echo ${1%nine}eight;;
25
-        *eight)     echo ${1%eight}seven;;
26
-        *seven)     echo ${1%seven}six;;
27
-        *six)       echo ${1%six}five;;
28
-        *five)      echo ${1%five}four;;
29
-        *four)      echo ${1%four}three;;
30
-        *three)     echo ${1%three}two;;
31
-        *two)       echo ${1%two}one;;
23
+    local numeral=$1    # number in word form
24
+    case $numeral in
25
+        *nine)      echo ${numeral%nine}eight;;
26
+        *eight)     echo ${numeral%eight}seven;;
27
+        *seven)     echo ${numeral%seven}six;;
28
+        *six)       echo ${numeral%six}five;;
29
+        *five)      echo ${numeral%five}four;;
30
+        *four)      echo ${numeral%four}three;;
31
+        *three)     echo ${numeral%three}two;;
32
+        *two)       echo ${numeral%two}one;;
32
         one)        echo zero;;
33
         one)        echo zero;;
33
-        *-one)      echo ${1%-one};;
34
-        *one)       echo ${1%one};;
34
+        *-one)      echo ${numeral%-one};;
35
+        *one)       echo ${numeral%one};;
35
         ten)        echo nine;;
36
         ten)        echo nine;;
36
         eleven)     echo ten;;
37
         eleven)     echo ten;;
37
         twelve)     echo eleven;;
38
         twelve)     echo eleven;;
38
-        *teen)      teenpred $1;;
39
-        *ty)        tenspred $1;;
39
+        *teen)      teenpred $numeral;;
40
+        *ty)        tenspred $numeral;;
40
         zero)       echo "";  #to terminate
41
         zero)       echo "";  #to terminate
41
     esac
42
     esac
42
 }
43
 }
45
     #
46
     #
46
     # Get predecessor of a teen $1
47
     # Get predecessor of a teen $1
47
     #
48
     #
48
-    case $1 in
49
+    local numeral=$1    # number in word form
50
+    case $numeral in
49
         thirteen)   echo twelve;;
51
         thirteen)   echo twelve;;
50
-        *)          echo $(crunchprefix $(pred $(uncrunchprefix ${1%teen})))teen;;
52
+        *)          echo $(crunchprefix $(pred $(uncrunchprefix ${numeral%teen})))teen;;
51
     esac
53
     esac
52
 }
54
 }
53
 
55
 
55
     #
57
     #
56
     # Get predecessor of a multiple of ten $1
58
     # Get predecessor of a multiple of ten $1
57
     #
59
     #
58
-    case $1 in
60
+    local numeral=$1    # number in word form
61
+    case $numeral in
59
         twenty)     echo nineteen;;
62
         twenty)     echo nineteen;;
60
-        *) echo $(crunchprefix --tens $(pred $(uncrunchprefix ${1%ty})))ty-nine;;
63
+        *) echo $(crunchprefix --tens $(pred $(uncrunchprefix ${numeral%ty})))ty-nine;;
61
     esac
64
     esac
62
 }
65
 }
63
 
66
 
69
     #
72
     #
70
     # option `--tens` - multiples of ten are a bit different
73
     # option `--tens` - multiples of ten are a bit different
71
     #
74
     #
75
+    local numeral       # number in word form
76
+    local tensop        # 'true' if --tens option is active
72
     [ $1 = --tens ] && { tensop=true; shift; }
77
     [ $1 = --tens ] && { tensop=true; shift; }
73
-    case $1 in
74
-        two)    [ -n "$tensop" ] && echo twen || echo $1;;
78
+    numeral=$1
79
+    case $numeral in
80
+        two)    [ -n "$tensop" ] && echo twen || echo $numeral;;
75
         three)  echo thir;;
81
         three)  echo thir;;
76
-        four)   [ -n "$tensop" ] && echo 'for' || echo $1;;
82
+        four)   [ -n "$tensop" ] && echo 'for' || echo $numeral;;
77
         five)   echo fif;;
83
         five)   echo fif;;
78
-        eight)  [ -n "$tensop" ] && echo eigh || echo $1;;
79
-        *)      echo $1;;
84
+        eight)  [ -n "$tensop" ] && echo eigh || echo $numeral;;
85
+        *)      echo $numeral;;
80
     esac
86
     esac
81
 }
87
 }
82
 
88
 
84
     #
90
     #
85
     # Reverse crunchprefix $1
91
     # Reverse crunchprefix $1
86
     #
92
     #
87
-    case $1 in
93
+    local prefix=$1     # numeral crunch-prefix
94
+    case $prefix in
88
         twen)   echo two;;
95
         twen)   echo two;;
89
         thir)   echo three;;
96
         thir)   echo three;;
90
         'for')  echo four;;
97
         'for')  echo four;;
91
         fif)    echo five;;
98
         fif)    echo five;;
92
         eigh)   echo eight;;
99
         eigh)   echo eight;;
93
-        *)      echo $1;;
100
+        *)      echo $prefix;;
94
     esac
101
     esac
95
 }
102
 }
96
 
103
 
98
     #
105
     #
99
     # Apply peculiarities of English grammar to text on stdin
106
     # Apply peculiarities of English grammar to text on stdin
100
     #
107
     #
101
-    local oneBottle=false  #can effect the following line
108
+    local oneBottle=false   # 'true' if this line affects the following line
109
+    local line              # every line
102
     while read line; do
110
     while read line; do
103
         line="${line/one more bottles/one more bottle}"
111
         line="${line/one more bottles/one more bottle}"
104
         case "$line" in
112
         case "$line" in
123
     #
131
     #
124
     # Fix capitalization of each line on stdin
132
     # Fix capitalization of each line on stdin
125
     #
133
     #
134
+    local line      # every line
126
     while read line; do
135
     while read line; do
127
         echo -n ${line:0:1} | tr '[:lower:]' '[:upper:]'
136
         echo -n ${line:0:1} | tr '[:lower:]' '[:upper:]'
128
         echo ${line#?}
137
         echo ${line#?}
133
     #
142
     #
134
     # Add punctuation to each line on stdin
143
     # Add punctuation to each line on stdin
135
     #
144
     #
145
+    local line      # every line
136
     while read line; do
146
     while read line; do
137
         case "${line}" in
147
         case "${line}" in
138
             [Ii]f*)     echo ${line},;;
148
             [Ii]f*)     echo ${line},;;
146
     #
156
     #
147
     # Write one verse with number $1
157
     # Write one verse with number $1
148
     #
158
     #
149
-    local nb=$1
159
+    local nb=$1     # numeral
150
     echo $nb bottles of beer on the wall
160
     echo $nb bottles of beer on the wall
151
     echo $nb bottles of beer
161
     echo $nb bottles of beer
152
     if [ $nb = zero ]; then
162
     if [ $nb = zero ]; then
163
     #
173
     #
164
     # Make text on stdin nice
174
     # Make text on stdin nice
165
     #
175
     #
176
+    local first     # first word of a line
177
+    local rest      # remainder of a line
178
+    local syl       # number of syllables in the first word
166
     while read first rest; do
179
     while read first rest; do
167
         case "$rest" in
180
         case "$rest" in
168
             *beer*)
181
             *beer*)
169
                 first=${first/zero/no}
182
                 first=${first/zero/no}
170
-                local syl=$(syllables ${first% *})
183
+                syl=$(syllables ${first% *})
171
                 case $syl in  #improve meter
184
                 case $syl in  #improve meter
172
                     1|2)    echo $first 'more' $rest;;
185
                     1|2)    echo $first 'more' $rest;;
173
                     *)      echo $first $rest;;
186
                     *)      echo $first $rest;;
182
     #
195
     #
183
     # Estimate number of syllables in word $1
196
     # Estimate number of syllables in word $1
184
     #
197
     #
198
+    local word=$1   # word (numeral) to do the estimation on
185
     local n=1
199
     local n=1
186
-    case $1 in
200
+    case $word in
187
         eleven)     n=2;; #sounds better if not considered 3
201
         eleven)     n=2;; #sounds better if not considered 3
188
         *teen)      n=2;;
202
         *teen)      n=2;;
189
         *ty)        n=2;;
203
         *ty)        n=2;;
190
         *-*)        n=3;; #don't care about more than 3
204
         *-*)        n=3;; #don't care about more than 3
191
     esac
205
     esac
192
-    case $1 in
206
+    case $word in
193
         *seven*)    let $((n = n+1));;
207
         *seven*)    let $((n = n+1));;
194
     esac
208
     esac
195
     echo $n
209
     echo $n
196
 }
210
 }
197
 
211
 
198
 main() {
212
 main() {
213
+    local breakLine             # bottle-break (or not) line
214
+    local standardBreakLine     # ^^ standard variant
215
+    local wastefulBreakLine     # ^^ wasteful (bottle breaks) variant
216
+    local nb            # beginning number
199
     standardBreakLine="Take one down and pass it around"
217
     standardBreakLine="Take one down and pass it around"
200
     wastefulBreakLine="If one of those bottles should happen to fall"
218
     wastefulBreakLine="If one of those bottles should happen to fall"
201
-    breakLine=$([ "$1" = --careless ] \
202
-     && echo $wastefulBreakLine \
203
-     || echo $standardBreakLine)
204
-
219
+    breakLine=$(
220
+        [ "$1" = --careless ] \
221
+         && echo $wastefulBreakLine \
222
+         || echo $standardBreakLine
223
+    )
205
     nb=ninety-nine
224
     nb=ninety-nine
206
-
207
     while [ -n "$nb" ]; do
225
     while [ -n "$nb" ]; do
208
         verse $nb
226
         verse $nb
209
         echo
227
         echo