📄 contributed-scripts.html
字号:
44 | tr -d wh \ 45 | tr $val1 1 | tr $val2 2 | tr $val3 3 \ 46 | tr $val4 4 | tr $val5 5 | tr $val6 6 \ 47 | tr -s 123456 \ 48 | tr -d aeiouy ) 49 50 # Assign letter values. 51 # Remove duplicate numbers, except when separated by vowels. 52 # Ignore vowels, except as separators, so delete them last. 53 # Ignore 'w' and 'h', even as separators, so delete them first. 54 # 55 # The above command substitution lays more pipe than a plumber <g>. 56 57 } 58 59 60 input_name="$1" 61 echo 62 echo "Name = $input_name" 63 64 65 # Change all characters of name input to lowercase. 66 # ------------------------------------------------ 67 name=$( echo $input_name | tr A-Z a-z ) 68 # ------------------------------------------------ 69 # Just in case argument to script is mixed case. 70 71 72 # Prefix of soundex code: first letter of name. 73 # -------------------------------------------- 74 75 76 char_pos=0 # Initialize character position. 77 prefix0=${name:$char_pos:1} 78 prefix=`echo $prefix0 | tr a-z A-Z` 79 # Uppercase 1st letter of soundex. 80 81 let "char_pos += 1" # Bump character position to 2nd letter of name. 82 name1=${name:$char_pos} 83 84 85 # ++++++++++++++++++++++++++ Exception Patch +++++++++++++++++++++++++++++++++ 86 # Now, we run both the input name and the name shifted one char to the right 87 #+ through the value-assigning function. 88 # If we get the same value out, that means that the first two characters 89 #+ of the name have the same value assigned, and that one should cancel. 90 # However, we also need to test whether the first letter of the name 91 #+ is a vowel or 'w' or 'h', because otherwise this would bollix things up. 92 93 char1=`echo $prefix | tr A-Z a-z` # First letter of name, lowercased. 94 95 assign_value $name 96 s1=$value 97 assign_value $name1 98 s2=$value 99 assign_value $char1 100 s3=$value 101 s3=9$s3 # If first letter of name is a vowel 102 #+ or 'w' or 'h', 103 #+ then its "value" will be null (unset). 104 #+ Therefore, set it to 9, an otherwise 105 #+ unused value, which can be tested for. 106 107 108 if [[ "$s1" -ne "$s2" || "$s3" -eq 9 ]] 109 then 110 suffix=$s2 111 else 112 suffix=${s2:$char_pos} 113 fi 114 # ++++++++++++++++++++++ end Exception Patch +++++++++++++++++++++++++++++++++ 115 116 117 padding=000 # Use at most 3 zeroes to pad. 118 119 120 soun=$prefix$suffix$padding # Pad with zeroes. 121 122 MAXLEN=4 # Truncate to maximum of 4 chars. 123 soundex=${soun:0:$MAXLEN} 124 125 echo "Soundex = $soundex" 126 127 echo 128 129 # The soundex code is a method of indexing and classifying names 130 #+ by grouping together the ones that sound alike. 131 # The soundex code for a given name is the first letter of the name, 132 #+ followed by a calculated three-number code. 133 # Similar sounding names should have almost the same soundex codes. 134 135 # Examples: 136 # Smith and Smythe both have a "S-530" soundex. 137 # Harrison = H-625 138 # Hargison = H-622 139 # Harriman = H-655 140 141 # This works out fairly well in practice, but there are numerous anomalies. 142 # 143 # 144 # The U.S. Census and certain other governmental agencies use soundex, 145 # as do genealogical researchers. 146 # 147 # For more information, 148 #+ see the "National Archives and Records Administration home page", 149 #+ http://www.nara.gov/genealogy/soundex/soundex.html 150 151 152 153 # Exercise: 154 # -------- 155 # Simplify the "Exception Patch" section of this script. 156 157 exit 0</PRE></TD></TR></TABLE><HR></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="LIFESLOW"></A><P><B>Example A-10. <SPANCLASS="QUOTE">"Game of Life"</SPAN></B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 # life.sh: "Life in the Slow Lane" 3 # Version 2: Patched by Daniel Albers 4 #+ to allow non-square grids as input. 5 6 # ##################################################################### # 7 # This is the Bash script version of John Conway's "Game of Life". # 8 # "Life" is a simple implementation of cellular automata. # 9 # --------------------------------------------------------------------- # 10 # On a rectangular grid, let each "cell" be either "living" or "dead". # 11 # Designate a living cell with a dot, and a dead one with a blank space.# 12 # Begin with an arbitrarily drawn dot-and-blank grid, # 13 #+ and let this be the starting generation, "generation 0". # 14 # Determine each successive generation by the following rules: # 15 # 1) Each cell has 8 neighbors, the adjoining cells # 16 #+ left, right, top, bottom, and the 4 diagonals. # 17 # 123 # 18 # 4*5 # 19 # 678 # 20 # # 21 # 2) A living cell with either 2 or 3 living neighbors remains alive. # 22 # 3) A dead cell with 3 living neighbors becomes alive (a "birth"). # 23 SURVIVE=2 # 24 BIRTH=3 # 25 # 4) All other cases result in a dead cell for the next generation. # 26 # ##################################################################### # 27 28 29 startfile=gen0 # Read the starting generation from the file "gen0". 30 # Default, if no other file specified when invoking script. 31 # 32 if [ -n "$1" ] # Specify another "generation 0" file. 33 then 34 if [ -e "$1" ] # Check for existence. 35 then 36 startfile="$1" 37 fi 38 fi 39 40 41 ALIVE1=. 42 DEAD1=_ 43 # Represent living and "dead" cells in the start-up file. 44 45 # ---------------------------------------------------------- # 46 # This script uses a 10 x 10 grid (may be increased, 47 #+ but a large grid will will cause very slow execution). 48 ROWS=10 49 COLS=10 50 # Change above two variables to match grid size, if necessary. 51 # ---------------------------------------------------------- # 52 53 GENERATIONS=10 # How many generations to cycle through. 54 # Adjust this upwards, 55 #+ if you have time on your hands. 56 57 NONE_ALIVE=80 # Exit status on premature bailout, 58 #+ if no cells left alive. 59 TRUE=0 60 FALSE=1 61 ALIVE=0 62 DEAD=1 63 64 avar= # Global; holds current generation. 65 generation=0 # Initialize generation count. 66 67 # ================================================================= 68 69 70 let "cells = $ROWS * $COLS" 71 # How many cells. 72 73 declare -a initial # Arrays containing "cells". 74 declare -a current 75 76 display () 77 { 78 79 alive=0 # How many cells "alive" at any given time. 80 # Initially zero. 81 82 declare -a arr 83 arr=( `echo "$1"` ) # Convert passed arg to array. 84 85 element_count=${#arr[*]} 86 87 local i 88 local rowcheck 89 90 for ((i=0; i<$element_count; i++)) 91 do 92 93 # Insert newline at end of each row. 94 let "rowcheck = $i % COLS" 95 if [ "$rowcheck" -eq 0 ] 96 then 97 echo # Newline. 98 echo -n " " # Indent. 99 fi 100 101 cell=${arr[i]} 102 103 if [ "$cell" = . ] 104 then 105 let "alive += 1" 106 fi 107 108 echo -n "$cell" | sed -e 's/_/ /g' 109 # Print out array and change underscores to spaces. 110 done 111 112 return 113 114 } 115 116 IsValid () # Test whether cell coordinate valid. 117 { 118 119 if [ -z "$1" -o -z "$2" ] # Mandatory arguments missing? 120 then 121 return $FALSE 122 fi 123 124 local row 125 local lower_limit=0 # Disallow negative coordinate. 126 local upper_limit 127 local left 128 local right 129 130 let "upper_limit = $ROWS * $COLS - 1" # Total number of cells. 131 132 133 if [ "$1" -lt "$lower_limit" -o "$1" -gt "$upper_limit" ] 134 then 135 return $FALSE # Out of array bounds. 136 fi 137 138 row=$2 139 let "left = $row * $COLS" # Left limit. 140 let "right = $left + $COLS - 1" # Right limit. 141 142 if [ "$1" -lt "$left" -o "$1" -gt "$right" ] 143 then 144 return $FALSE # Beyond row boundary. 145 fi 146 147 return $TRUE # Valid coordinate. 148 149 } 150 151 152 IsAlive () # Test whether cell is alive. 153 # Takes array, cell number, state of cell as arguments. 154 { 155 GetCount "$1" $2 # Get alive cell count in neighborhood. 156 local nhbd=$? 157 158 159 if [ "$nhbd" -eq "$BIRTH" ] # Alive in any case. 160 then 161 return $ALIVE 162 fi 163 164 if [ "$3" = "." -a "$nhbd" -eq "$SURVIVE" ] 165 then # Alive only if previously alive. 166 return $ALIVE 167 fi 168 169 return $DEAD # Default. 170 171 } 172 173 174 GetCount () # Count live cells in passed cell's neighborhood. 175 # Two arguments needed: 176 # $1) variable holding array 177 # $2) cell number 178 { 179 local cell_number=$2 180 local array 181 local top 182 local center 183 local bottom 184 local r 185 local row 186 local i 187 local t_top 188 local t_cen 189 local t_bot 190 local count=0 191 local ROW_NHBD=3 192 193 array=( `echo "$1"` ) 194 195 let "top = $cell_number - $COLS - 1" # Set up cell neighborhood. 196 let "center = $cell_number - 1" 197 let "bottom = $cell_number + $COLS - 1" 198 let "r = $cell_number / $COLS" 199 200 for ((i=0; i<$ROW_NHBD; i++)) # Traverse from left to right. 201 do 202 let "t_top = $top + $i" 203 let "t_cen = $center + $i" 204 let "t_bot = $bottom + $i" 205 206 207 let "row = $r" # Count center row of neighborhood. 208 IsValid $t_cen $row # Valid cell position? 209 if [ $? -eq "$TRUE" ] 210 then 211 if [ ${array[$t_cen]} = "$ALIVE1" ] # Is it alive? 212 then # Yes? 213 let "count += 1" # Increment count. 214 fi 215 fi 216 217 let "row = $r - 1" # Count top row. 218 IsValid $t_top $row 219 if [ $? -eq "$TRUE" ] 220 then 221 if [ ${array[$t_top]} = "$ALIVE1" ] 222 then 223 let "count += 1" 224 fi 225 fi 226 227 let "row = $r + 1" # Count bottom row. 228 IsValid $t_bot $row 229 if [ $? -eq "$TRUE" ] 230 then 231 if [ ${array[$t_bot]} = "$ALIVE1" ] 232 then 233 let "count += 1" 234 fi 235 fi 236 237 done 238 239 240 if [ ${array[$cell_number]} = "$ALIVE1" ] 241 then 242 let "count -= 1" # Make sure value of tested cell itself 243 fi #+ is not counted. 244 245 246 return $count 247 248 } 249 250 next_gen () # Update generation array. 251 { 252 253 local array 254 local i=0 255 256 array=( `echo "$1"` ) # Convert passed arg to array. 257 258 while [ "$i" -lt "$cells" ] 259 do 260 IsAlive "$1" $i ${array[$i]} # Is cell alive? 261 if [ $? -eq "$ALIVE" ] 262 then # If alive, then 263 array[$i]=. #+ represent the cell as a period. 264 else 265 array[$i]="_" # Otherwise underscore 266 fi #+ (which will later be converted to space). 267 let "i += 1" 268 done 269 270 271 # let "generation += 1" # Increment generation count. 272 # Why was the above line commented out? 273 274 275 # Set variable to pass as parameter to "display" function. 276 avar=`echo ${array[@]}` # Convert array back to string variable. 277 display "$avar" # Display it. 278 echo; echo
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -