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

📄 contributed-scripts.html

📁 一本完整的描述Unix Shell 编程的工具书的所有范例
💻 HTML
📖 第 1 页 / 共 5 页
字号:
  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><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&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;  if [ -e "$1" ] # Check for existence.  35&nbsp;  then  36&nbsp;    startfile="$1"  37&nbsp;  fi    38&nbsp;fi    39&nbsp;  40&nbsp;  41&nbsp;ALIVE1=.  42&nbsp;DEAD1=_  43&nbsp;                 # Represent living and "dead" cells in the start-up file.  44&nbsp;  45&nbsp;#  ---------------------------------------------------------- #  46&nbsp;#  This script uses a 10 x 10 grid (may be increased,  47&nbsp;#+ but a large grid will will cause very slow execution).  48&nbsp;ROWS=10  49&nbsp;COLS=10  50&nbsp;#  Change above two variables to match grid size, if necessary.  51&nbsp;#  ---------------------------------------------------------- #  52&nbsp;  53&nbsp;GENERATIONS=10          #  How many generations to cycle through.  54&nbsp;                        #  Adjust this upwards,  55&nbsp;                        #+ if you have time on your hands.  56&nbsp;  57&nbsp;NONE_ALIVE=80           #  Exit status on premature bailout,  58&nbsp;                        #+ if no cells left alive.  59&nbsp;TRUE=0  60&nbsp;FALSE=1  61&nbsp;ALIVE=0  62&nbsp;DEAD=1  63&nbsp;  64&nbsp;avar=                   # Global; holds current generation.  65&nbsp;generation=0            # Initialize generation count.  66&nbsp;  67&nbsp;# =================================================================  68&nbsp;  69&nbsp;  70&nbsp;let "cells = $ROWS * $COLS"  71&nbsp;                        # How many cells.  72&nbsp;  73&nbsp;declare -a initial      # Arrays containing "cells".  74&nbsp;declare -a current  75&nbsp;  76&nbsp;display ()  77&nbsp;{  78&nbsp;  79&nbsp;alive=0                 # How many cells "alive" at any given time.  80&nbsp;                        # Initially zero.  81&nbsp;  82&nbsp;declare -a arr  83&nbsp;arr=( `echo "$1"` )     # Convert passed arg to array.  84&nbsp;  85&nbsp;element_count=${#arr[*]}  86&nbsp;  87&nbsp;local i  88&nbsp;local rowcheck  89&nbsp;  90&nbsp;for ((i=0; i&#60;$element_count; i++))  91&nbsp;do  92&nbsp;  93&nbsp;  # Insert newline at end of each row.  94&nbsp;  let "rowcheck = $i % COLS"  95&nbsp;  if [ "$rowcheck" -eq 0 ]  96&nbsp;  then  97&nbsp;    echo                # Newline.  98&nbsp;    echo -n "      "    # Indent.  99&nbsp;  fi   100&nbsp; 101&nbsp;  cell=${arr[i]} 102&nbsp; 103&nbsp;  if [ "$cell" = . ] 104&nbsp;  then 105&nbsp;    let "alive += 1" 106&nbsp;  fi   107&nbsp; 108&nbsp;  echo -n "$cell" | sed -e 's/_/ /g' 109&nbsp;  # Print out array and change underscores to spaces. 110&nbsp;done   111&nbsp; 112&nbsp;return 113&nbsp; 114&nbsp;} 115&nbsp; 116&nbsp;IsValid ()                            # Test whether cell coordinate valid. 117&nbsp;{ 118&nbsp; 119&nbsp;  if [ -z "$1"  -o -z "$2" ]          # Mandatory arguments missing? 120&nbsp;  then 121&nbsp;    return $FALSE 122&nbsp;  fi 123&nbsp; 124&nbsp;local row 125&nbsp;local lower_limit=0                   # Disallow negative coordinate. 126&nbsp;local upper_limit 127&nbsp;local left 128&nbsp;local right 129&nbsp; 130&nbsp;let "upper_limit = $ROWS * $COLS - 1" # Total number of cells. 131&nbsp; 132&nbsp; 133&nbsp;if [ "$1" -lt "$lower_limit" -o "$1" -gt "$upper_limit" ] 134&nbsp;then 135&nbsp;  return $FALSE                       # Out of array bounds. 136&nbsp;fi   137&nbsp; 138&nbsp;row=$2 139&nbsp;let "left = $row * $COLS"             # Left limit. 140&nbsp;let "right = $left + $COLS - 1"       # Right limit. 141&nbsp; 142&nbsp;if [ "$1" -lt "$left" -o "$1" -gt "$right" ] 143&nbsp;then 144&nbsp;  return $FALSE                       # Beyond row boundary. 145&nbsp;fi   146&nbsp; 147&nbsp;return $TRUE                          # Valid coordinate. 148&nbsp; 149&nbsp;}   150&nbsp; 151&nbsp; 152&nbsp;IsAlive ()              # Test whether cell is alive. 153&nbsp;                        # Takes array, cell number, state of cell as arguments. 154&nbsp;{ 155&nbsp;  GetCount "$1" $2      # Get alive cell count in neighborhood. 156&nbsp;  local nhbd=$? 157&nbsp; 158&nbsp; 159&nbsp;  if [ "$nhbd" -eq "$BIRTH" ]  # Alive in any case. 160&nbsp;  then 161&nbsp;    return $ALIVE 162&nbsp;  fi 163&nbsp; 164&nbsp;  if [ "$3" = "." -a "$nhbd" -eq "$SURVIVE" ] 165&nbsp;  then                  # Alive only if previously alive. 166&nbsp;    return $ALIVE 167&nbsp;  fi   168&nbsp; 169&nbsp;  return $DEAD          # Default. 170&nbsp; 171&nbsp;}   172&nbsp; 173&nbsp; 174&nbsp;GetCount ()             # Count live cells in passed cell's neighborhood. 175&nbsp;                        # Two arguments needed: 176&nbsp;			# $1) variable holding array 177&nbsp;			# $2) cell number 178&nbsp;{ 179&nbsp;  local cell_number=$2 180&nbsp;  local array 181&nbsp;  local top 182&nbsp;  local center 183&nbsp;  local bottom 184&nbsp;  local r 185&nbsp;  local row 186&nbsp;  local i 187&nbsp;  local t_top 188&nbsp;  local t_cen 189&nbsp;  local t_bot 190&nbsp;  local count=0 191&nbsp;  local ROW_NHBD=3 192&nbsp; 193&nbsp;  array=( `echo "$1"` ) 194&nbsp; 195&nbsp;  let "top = $cell_number - $COLS - 1"    # Set up cell neighborhood. 196&nbsp;  let "center = $cell_number - 1" 197&nbsp;  let "bottom = $cell_number + $COLS - 1" 198&nbsp;  let "r = $cell_number / $COLS" 199&nbsp; 200&nbsp;  for ((i=0; i&#60;$ROW_NHBD; i++))           # Traverse from left to right.  201&nbsp;  do 202&nbsp;    let "t_top = $top + $i" 203&nbsp;    let "t_cen = $center + $i" 204&nbsp;    let "t_bot = $bottom + $i" 205&nbsp; 206&nbsp; 207&nbsp;    let "row = $r"                        # Count center row of neighborhood. 208&nbsp;    IsValid $t_cen $row                   # Valid cell position? 209&nbsp;    if [ $? -eq "$TRUE" ] 210&nbsp;    then 211&nbsp;      if [ ${array[$t_cen]} = "$ALIVE1" ] # Is it alive? 212&nbsp;      then                                # Yes? 213&nbsp;        let "count += 1"                  # Increment count. 214&nbsp;      fi	 215&nbsp;    fi   216&nbsp; 217&nbsp;    let "row = $r - 1"                    # Count top row.           218&nbsp;    IsValid $t_top $row 219&nbsp;    if [ $? -eq "$TRUE" ] 220&nbsp;    then 221&nbsp;      if [ ${array[$t_top]} = "$ALIVE1" ]  222&nbsp;      then 223&nbsp;        let "count += 1" 224&nbsp;      fi	 225&nbsp;    fi   226&nbsp; 227&nbsp;    let "row = $r + 1"                    # Count bottom row. 228&nbsp;    IsValid $t_bot $row 229&nbsp;    if [ $? -eq "$TRUE" ] 230&nbsp;    then 231&nbsp;      if [ ${array[$t_bot]} = "$ALIVE1" ]  232&nbsp;      then 233&nbsp;        let "count += 1" 234&nbsp;      fi	 235&nbsp;    fi   236&nbsp; 237&nbsp;  done   238&nbsp; 239&nbsp; 240&nbsp;  if [ ${array[$cell_number]} = "$ALIVE1" ] 241&nbsp;  then 242&nbsp;    let "count -= 1"        #  Make sure value of tested cell itself 243&nbsp;  fi                        #+ is not counted. 244&nbsp; 245&nbsp; 246&nbsp;  return $count 247&nbsp;   248&nbsp;} 249&nbsp; 250&nbsp;next_gen ()               # Update generation array. 251&nbsp;{ 252&nbsp; 253&nbsp;local array 254&nbsp;local i=0 255&nbsp; 256&nbsp;array=( `echo "$1"` )     # Convert passed arg to array. 257&nbsp; 258&nbsp;while [ "$i" -lt "$cells" ] 259&nbsp;do 260&nbsp;  IsAlive "$1" $i ${array[$i]}   # Is cell alive? 261&nbsp;  if [ $? -eq "$ALIVE" ] 262&nbsp;  then                           #  If alive, then 263&nbsp;    array[$i]=.                  #+ represent the cell as a period. 264&nbsp;  else   265&nbsp;    array[$i]="_"                #  Otherwise underscore 266&nbsp;   fi                            #+ (which will later be converted to space).   267&nbsp;  let "i += 1"  268&nbsp;done    269&nbsp; 270&nbsp; 271&nbsp;# let "generation += 1"   # Increment generation count. 272&nbsp;# Why was the above line commented out? 273&nbsp; 274&nbsp; 275&nbsp;# Set variable to pass as parameter to "display" function. 276&nbsp;avar=`echo ${array[@]}`   # Convert array back to string variable. 277&nbsp;display "$avar"           # Display it. 278&nbsp;echo; echo

⌨️ 快捷键说明

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