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

📄 contributed-scripts.html

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

⌨️ 快捷键说明

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