⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 contributed-scripts.html

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

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -