📄 contributed-scripts.html
字号:
249 250 251 if [ ${array[$cell_number]} = "$ALIVE1" ] 252 then 253 let "count -= 1" # Make sure value of tested cell itself 254 fi #+ is not counted. 255 256 257 return $count 258 259 } 260 261 next_gen () # Update generation array. 262 { 263 264 local array 265 local i=0 266 267 array=( `echo "$1"` ) # Convert passed arg to array. 268 269 while [ "$i" -lt "$cells" ] 270 do 271 IsAlive "$1" $i ${array[$i]} # Is cell alive? 272 if [ $? -eq "$ALIVE" ] 273 then # If alive, then 274 array[$i]=. #+ represent the cell as a period. 275 else 276 array[$i]="_" # Otherwise underscore 277 fi #+ (which will later be converted to space). 278 let "i += 1" 279 done 280 281 282 # let "generation += 1" # Increment generation count. 283 # Why was the above line commented out? 284 285 286 # Set variable to pass as parameter to "display" function. 287 avar=`echo ${array[@]}` # Convert array back to string variable. 288 display "$avar" # Display it. 289 echo; echo 290 echo "Generation $generation - $alive alive" 291 292 if [ "$alive" -eq 0 ] 293 then 294 echo 295 echo "Premature exit: no more cells alive!" 296 exit $NONE_ALIVE # No point in continuing 297 fi #+ if no live cells. 298 299 } 300 301 302 # ========================================================= 303 304 # main () 305 306 # Load initial array with contents of startup file. 307 initial=( `cat "$startfile" | sed -e '/#/d' | tr -d '\n' |\ 308 sed -e 's/\./\. /g' -e 's/_/_ /g'` ) 309 # Delete lines containing '#' comment character. 310 # Remove linefeeds and insert space between elements. 311 312 clear # Clear screen. 313 314 echo # Title 315 echo "=======================" 316 echo " $GENERATIONS generations" 317 echo " of" 318 echo "\"Life in the Slow Lane\"" 319 echo "=======================" 320 321 322 # -------- Display first generation. -------- 323 Gen0=`echo ${initial[@]}` 324 display "$Gen0" # Display only. 325 echo; echo 326 echo "Generation $generation - $alive alive" 327 # ------------------------------------------- 328 329 330 let "generation += 1" # Increment generation count. 331 echo 332 333 # ------- Display second generation. ------- 334 Cur=`echo ${initial[@]}` 335 next_gen "$Cur" # Update & display. 336 # ------------------------------------------ 337 338 let "generation += 1" # Increment generation count. 339 340 # ------ Main loop for displaying subsequent generations ------ 341 while [ "$generation" -le "$GENERATIONS" ] 342 do 343 Cur="$avar" 344 next_gen "$Cur" 345 let "generation += 1" 346 done 347 # ============================================================== 348 349 echo 350 351 exit 0 # END 352 353 354 355 # The grid in this script has a "boundary problem." 356 # The the top, bottom, and sides border on a void of dead cells. 357 # Exercise: Change the script to have the grid wrap around, 358 # + so that the left and right sides will "touch," 359 # + as will the top and bottom. 360 # 361 # Exercise: Create a new "gen0" file to seed this script. 362 # Use a 12 x 16 grid, instead of the original 10 x 10 one. 363 # Make the necessary changes to the script, 364 #+ so it will run with the altered file. 365 # 366 # Exercise: Modify this script so that it can determine the grid size 367 #+ from the "gen0" file, and set any variables necessary 368 #+ for the script to run. 369 # This would make unnecessary any changes to variables 370 #+ in the script for an altered grid size.</PRE></TD></TR></TABLE><HR></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="GEN0DATA"></A><P><B>Example A-11. Data file for <ICLASS="FIRSTTERM">Game of Life</I></B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 # gen0 2 # 3 # This is an example "generation 0" start-up file for "life.sh". 4 # -------------------------------------------------------------- 5 # The "gen0" file is a 10 x 10 grid using a period (.) for live cells, 6 #+ and an underscore (_) for dead ones. We cannot simply use spaces 7 #+ for dead cells in this file because of a peculiarity in Bash arrays. 8 # [Exercise for the reader: explain this.] 9 # 10 # Lines beginning with a '#' are comments, and the script ignores them. 11 __.__..___ 12 ___._.____ 13 ____.___.. 14 _._______. 15 ____._____ 16 ..__...___ 17 ____._____ 18 ___...____ 19 __.._..___ 20 _..___..__</PRE></TD></TR></TABLE><HR></DIV><P>+++</P><P>The following two scripts are by Mark Moraes of the University of Toronto. See the file <TTCLASS="FILENAME">Moraes-COPYRIGHT</TT> for permissions and restrictions. This file is included in the combined <AHREF="index.html#WHERE_TARBALL">HTML/source tarball</A> of the <SPANCLASS="emphasis"><ICLASS="EMPHASIS">ABS Guide</I></SPAN>.</P><DIVCLASS="EXAMPLE"><HR><ANAME="BEHEAD"></A><P><B>Example A-12. <ICLASS="FIRSTTERM">behead</I>: Removing mail and news message headers</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #! /bin/sh 2 # Strips off the header from a mail/News message i.e. till the first 3 # empty line 4 # Mark Moraes, University of Toronto 5 6 # ==> These comments added by author of this document. 7 8 if [ $# -eq 0 ]; then 9 # ==> If no command line args present, then works on file redirected to stdin. 10 sed -e '1,/^$/d' -e '/^[ ]*$/d' 11 # --> Delete empty lines and all lines until 12 # --> first one beginning with white space. 13 else 14 # ==> If command line args present, then work on files named. 15 for i do 16 sed -e '1,/^$/d' -e '/^[ ]*$/d' $i 17 # --> Ditto, as above. 18 done 19 fi 20 21 # ==> Exercise: Add error checking and other options. 22 # ==> 23 # ==> Note that the small sed script repeats, except for the arg passed. 24 # ==> Does it make sense to embed it in a function? Why or why not?</PRE></TD></TR></TABLE><HR></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="FTPGET"></A><P><B>Example A-13. <ICLASS="FIRSTTERM">ftpget</I>: Downloading files via ftp</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #! /bin/sh 2 # $Id: ftpget,v 1.2 91/05/07 21:15:43 moraes Exp $ 3 # Script to perform batch anonymous ftp. Essentially converts a list of 4 # of command line arguments into input to ftp. 5 # ==> This script is nothing but a shell wrapper around "ftp" . . . 6 # Simple, and quick - written as a companion to ftplist 7 # -h specifies the remote host (default prep.ai.mit.edu) 8 # -d specifies the remote directory to cd to - you can provide a sequence 9 # of -d options - they will be cd'ed to in turn. If the paths are relative, 10 # make sure you get the sequence right. Be careful with relative paths - 11 # there are far too many symlinks nowadays. 12 # (default is the ftp login directory) 13 # -v turns on the verbose option of ftp, and shows all responses from the 14 # ftp server. 15 # -f remotefile[:localfile] gets the remote file into localfile 16 # -m pattern does an mget with the specified pattern. Remember to quote 17 # shell characters. 18 # -c does a local cd to the specified directory 19 # For example, 20 # ftpget -h expo.lcs.mit.edu -d contrib -f xplaces.shar:xplaces.sh \ 21 # -d ../pub/R3/fixes -c ~/fixes -m 'fix*' 22 # will get xplaces.shar from ~ftp/contrib on expo.lcs.mit.edu, and put it 23 # in xplaces.sh in the current working directory, and get all fixes from 24 # ~ftp/pub/R3/fixes and put them in the ~/fixes directory. 25 # Obviously, the sequence of the options is important, since the equivalent 26 # commands are executed by ftp in corresponding order 27 # 28 # Mark Moraes <moraes@csri.toronto.edu>, Feb 1, 1989 29 # 30 31 32 # ==> These comments added by author of this document. 33 34 # PATH=/local/bin:/usr/ucb:/usr/bin:/bin 35 # export PATH 36 # ==> Above 2 lines from original script probably superfluous. 37 38 E_BADARGS=65 39 40 TMPFILE=/tmp/ftp.$$ 41 # ==> Creates temp file, using process id of script ($$) 42 # ==> to construct filename. 43 44 SITE=`domainname`.toronto.edu 45 # ==> 'domainname' similar to 'hostname' 46 # ==> May rewrite this to parameterize this for general use. 47 48 usage="Usage: $0 [-h remotehost] [-d remotedirectory]... \ 49 [-f remfile:localfile]... [-c localdirectory] [-m filepattern] [-v]" 50 ftpflags="-i -n" 51 verbflag= 52 set -f # So we can use globbing in -m 53 set x `getopt vh:d:c:m:f: $*` 54 if [ $? != 0 ]; then 55 echo $usage 56 exit $E_BADARGS 57 fi 58 shift 59 trap 'rm -f ${TMPFILE} ; exit' 0 1 2 3 15 60 # ==> Signals: HUP INT (Ctl-C) QUIT TERM 61 # ==> Delete tempfile in case of abnormal exit from script. 62 echo "user anonymous ${USER-gnu}@${SITE} > ${TMPFILE}" 63 # ==> Added quotes (recommended in complex echoes). 64 echo binary >> ${TMPFILE} 65 for i in $* # ==> Parse command line args. 66 do 67 case $i in 68 -v) verbflag=-v; echo hash >> ${TMPFILE}; shift;; 69 -h) remhost=$2; shift 2;; 70 -d) echo cd $2 >> ${TMPFILE}; 71 if [ x${verbflag} != x ]; then 72 echo pwd >> ${TMPFILE}; 73 fi; 74 shift 2;; 75 -c) echo lcd $2 >> ${TMPFILE}; shift 2;; 76 -m) echo mget "$2" >> ${TMPFILE}; shift 2;; 77 -f) f1=`expr "$2" : "\([^:]*\).*"`; f2=`expr "$2" : "[^:]*:\(.*\)"`; 78 echo get ${f1} ${f2} >> ${TMPFILE}; shift 2;; 79 --) shift; break;; 80 esac 81 # ==> 'lcd' and 'mget' are ftp commands. See "man ftp" . . . 82 done 83 if [ $# -ne 0 ]; then 84 echo $usage 85 exit $E_BADARGS 86 # ==> Changed from "exit 2" to conform with style standard. 87 fi 88 if [ x${verbflag} != x ]; then 89 ftpflags="${ftpflags} -v" 90 fi 91 if [ x${remhost} = x ]; then 92 remhost=prep.ai.mit.edu 93 # ==> Change to match appropriate ftp site. 94 fi 95 echo quit >> ${TMPFILE} 96 # ==> All commands saved in tempfile. 97 98 ftp ${ftpflags} ${remhost} < ${TMPFILE} 99 # ==> Now, tempfile batch processed by ftp. 100 101 rm -f ${TMPFILE} 102 # ==> Finally, tempfile deleted (you may wish to copy it to a logfile). 103 104 105 # ==> Exercises: 106 # ==> --------- 107 # ==> 1) Add error checking. 108 # ==> 2) Add bells & whistles.</PRE></TD></TR></TABLE><HR></DIV><P>+</P><P>Antek Sawicki contributed the following script, which makes very clever use of the parameter substitution operators discussed in <AHREF="parameter-substitution.html">Section 9.3</A>.</P><P><ANAME="PW0"></A></P><DIVCLASS="EXAMPLE"><HR><ANAME="PW"></A><P><B>Example A-14. <ICLASS="FIRSTTERM">password</I>: Generating random 8-character passwords</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 # May need to be invoked with #!/bin/bash2 on older machines. 3 # 4 # Random password generator for Bash 2.x + 5 #+ by Antek Sawicki <tenox@tenox.tc>, 6 #+ who generously gave usage permission to the ABS Guide author. 7 # 8 # ==> Comments added by document author ==>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -