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

📄 contributed-scripts.html

📁 Shall高级编程
💻 HTML
📖 第 1 页 / 共 5 页
字号:
  15&nbsp;  16&nbsp;echo; echo "Copying the source CD to $OF."  17&nbsp;echo "This may take a while. Please be patient."  18&nbsp;  19&nbsp;dd if=$CDROM of=$OF bs=$BLOCKSIZE          # Raw device copy.  20&nbsp;  21&nbsp;  22&nbsp;echo; echo "Remove data CD."  23&nbsp;echo "Insert blank CDR."  24&nbsp;echo "Press ENTER when ready. "  25&nbsp;read ready                                 # Wait for input, $ready not used.  26&nbsp;  27&nbsp;echo "Copying $OF to CDR."  28&nbsp;  29&nbsp;cdrecord -v -isosize speed=$SPEED dev=$DEVICE $OF  30&nbsp;# Uses Joerg Schilling's "cdrecord" package (see its docs).  31&nbsp;# http://www.fokus.gmd.de/nthp/employees/schilling/cdrecord.html  32&nbsp;  33&nbsp;  34&nbsp;echo; echo "Done copying $OF to CDR on device $CDROM."  35&nbsp;  36&nbsp;echo "Do you want to erase the image file (y/n)? "  # Probably a huge file.  37&nbsp;read answer  38&nbsp;  39&nbsp;case "$answer" in  40&nbsp;[yY]) rm -f $OF  41&nbsp;      echo "$OF erased."  42&nbsp;      ;;  43&nbsp;*)    echo "$OF not erased.";;  44&nbsp;esac  45&nbsp;  46&nbsp;echo  47&nbsp;  48&nbsp;# Exercise:  49&nbsp;# Change the above "case" statement to also accept "yes" and "Yes" as input.  50&nbsp;  51&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="COLLATZ"></A><P><B>Example A-6. Collatz series</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# collatz.sh   3&nbsp;   4&nbsp;#  The notorious "hailstone" or Collatz series.   5&nbsp;#  -------------------------------------------   6&nbsp;#  1) Get the integer "seed" from the command line.   7&nbsp;#  2) NUMBER &#60;--- seed   8&nbsp;#  3) Print NUMBER.   9&nbsp;#  4)  If NUMBER is even, divide by 2, or  10&nbsp;#  5)+ if odd, multiply by 3 and add 1.  11&nbsp;#  6) NUMBER &#60;--- result   12&nbsp;#  7) Loop back to step 3 (for specified number of iterations).  13&nbsp;#  14&nbsp;#  The theory is that every sequence,  15&nbsp;#+ no matter how large the initial value,  16&nbsp;#+ eventually settles down to repeating "4,2,1..." cycles,  17&nbsp;#+ even after fluctuating through a wide range of values.  18&nbsp;#  19&nbsp;#  This is an instance of an "iterate,"  20&nbsp;#+ an operation that feeds its output back into the input.  21&nbsp;#  Sometimes the result is a "chaotic" series.  22&nbsp;  23&nbsp;  24&nbsp;MAX_ITERATIONS=200  25&nbsp;# For large seed numbers (&#62;32000), increase MAX_ITERATIONS.  26&nbsp;  27&nbsp;h=${1:-$$}                      #  Seed  28&nbsp;                                #  Use $PID as seed,  29&nbsp;                                #+ if not specified as command-line arg.  30&nbsp;  31&nbsp;echo  32&nbsp;echo "C($h) --- $MAX_ITERATIONS Iterations"  33&nbsp;echo  34&nbsp;  35&nbsp;for ((i=1; i&#60;=MAX_ITERATIONS; i++))  36&nbsp;do  37&nbsp;  38&nbsp;echo -n "$h	"  39&nbsp;#          ^^^^^  40&nbsp;#           tab  41&nbsp;  42&nbsp;  let "remainder = h % 2"  43&nbsp;  if [ "$remainder" -eq 0 ]   # Even?  44&nbsp;  then  45&nbsp;    let "h /= 2"              # Divide by 2.  46&nbsp;  else  47&nbsp;    let "h = h*3 + 1"         # Multiply by 3 and add 1.  48&nbsp;  fi  49&nbsp;  50&nbsp;  51&nbsp;COLUMNS=10                    # Output 10 values per line.  52&nbsp;let "line_break = i % $COLUMNS"  53&nbsp;if [ "$line_break" -eq 0 ]  54&nbsp;then  55&nbsp;  echo  56&nbsp;fi    57&nbsp;  58&nbsp;done  59&nbsp;  60&nbsp;echo  61&nbsp;  62&nbsp;#  For more information on this mathematical function,  63&nbsp;#+ see _Computers, Pattern, Chaos, and Beauty_, by Pickover, p. 185 ff.,  64&nbsp;#+ as listed in the bibliography.  65&nbsp;  66&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><P><ANAME="DAYSBETWEEN0"></A></P><DIVCLASS="EXAMPLE"><HR><ANAME="DAYSBETWEEN"></A><P><B>Example A-7. <ICLASS="FIRSTTERM">days-between</I>: Days between two        dates</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# days-between.sh:    Number of days between two dates.   3&nbsp;# Usage: ./days-between.sh [M]M/[D]D/YYYY [M]M/[D]D/YYYY   4&nbsp;#   5&nbsp;# Note: Script modified to account for changes in Bash, v. 2.05b +,   6&nbsp;#+      that closed the loophole permitting large negative   7&nbsp;#+      integer return values.   8&nbsp;   9&nbsp;ARGS=2                # Two command line parameters expected.  10&nbsp;E_PARAM_ERR=65        # Param error.  11&nbsp;  12&nbsp;REFYR=1600            # Reference year.  13&nbsp;CENTURY=100  14&nbsp;DIY=365  15&nbsp;ADJ_DIY=367           # Adjusted for leap year + fraction.  16&nbsp;MIY=12  17&nbsp;DIM=31  18&nbsp;LEAPCYCLE=4  19&nbsp;  20&nbsp;MAXRETVAL=255         #  Largest permissible  21&nbsp;                      #+ positive return value from a function.  22&nbsp;  23&nbsp;diff=                 # Declare global variable for date difference.  24&nbsp;value=                # Declare global variable for absolute value.  25&nbsp;day=                  # Declare globals for day, month, year.  26&nbsp;month=  27&nbsp;year=  28&nbsp;  29&nbsp;  30&nbsp;Param_Error ()        # Command line parameters wrong.  31&nbsp;{  32&nbsp;  echo "Usage: `basename $0` [M]M/[D]D/YYYY [M]M/[D]D/YYYY"  33&nbsp;  echo "       (date must be after 1/3/1600)"  34&nbsp;  exit $E_PARAM_ERR  35&nbsp;}    36&nbsp;  37&nbsp;  38&nbsp;Parse_Date ()                 # Parse date from command line params.  39&nbsp;{  40&nbsp;  month=${1%%/**}  41&nbsp;  dm=${1%/**}                 # Day and month.  42&nbsp;  day=${dm#*/}  43&nbsp;  let "year = `basename $1`"  # Not a filename, but works just the same.  44&nbsp;}    45&nbsp;  46&nbsp;  47&nbsp;check_date ()                 # Checks for invalid date(s) passed.  48&nbsp;{  49&nbsp;  [ "$day" -gt "$DIM" ] || [ "$month" -gt "$MIY" ] ||  50&nbsp;  [ "$year" -lt "$REFYR" ] &#38;&#38; Param_Error  51&nbsp;  # Exit script on bad value(s).  52&nbsp;  # Uses or-list / and-list.  53&nbsp;  #  54&nbsp;  # Exercise: Implement more rigorous date checking.  55&nbsp;}  56&nbsp;  57&nbsp;  58&nbsp;strip_leading_zero () #  Better to strip possible leading zero(s)  59&nbsp;{                     #+ from day and/or month  60&nbsp;  return ${1#0}       #+ since otherwise Bash will interpret them  61&nbsp;}                     #+ as octal values (POSIX.2, sect 2.9.2.1).  62&nbsp;  63&nbsp;  64&nbsp;day_index ()          # Gauss' Formula:  65&nbsp;{                     # Days from March 1, 1600 to date passed as param.  66&nbsp;                      #           ^^^^^^^^^^^^^  67&nbsp;  day=$1  68&nbsp;  month=$2  69&nbsp;  year=$3  70&nbsp;  71&nbsp;  let "month = $month - 2"  72&nbsp;  if [ "$month" -le 0 ]  73&nbsp;  then  74&nbsp;    let "month += 12"  75&nbsp;    let "year -= 1"  76&nbsp;  fi    77&nbsp;  78&nbsp;  let "year -= $REFYR"  79&nbsp;  let "indexyr = $year / $CENTURY"  80&nbsp;  81&nbsp;  82&nbsp;  let "Days = $DIY*$year + $year/$LEAPCYCLE - $indexyr \  83&nbsp;              + $indexyr/$LEAPCYCLE + $ADJ_DIY*$month/$MIY + $day - $DIM"  84&nbsp;  #  For an in-depth explanation of this algorithm, see  85&nbsp;  #+   http://weblogs.asp.net/pgreborio/archive/2005/01/06/347968.aspx  86&nbsp;  87&nbsp;  88&nbsp;  echo $Days  89&nbsp;  90&nbsp;}    91&nbsp;  92&nbsp;  93&nbsp;calculate_difference ()            # Difference between two day indices.  94&nbsp;{  95&nbsp;  let "diff = $1 - $2"             # Global variable.  96&nbsp;}    97&nbsp;  98&nbsp;  99&nbsp;abs ()                             #  Absolute value 100&nbsp;{                                  #  Uses global "value" variable. 101&nbsp;  if [ "$1" -lt 0 ]                #  If negative 102&nbsp;  then                             #+ then 103&nbsp;    let "value = 0 - $1"           #+ change sign, 104&nbsp;  else                             #+ else 105&nbsp;    let "value = $1"               #+ leave it alone. 106&nbsp;  fi 107&nbsp;} 108&nbsp; 109&nbsp; 110&nbsp; 111&nbsp;if [ $# -ne "$ARGS" ]              # Require two command line params. 112&nbsp;then 113&nbsp;  Param_Error 114&nbsp;fi   115&nbsp; 116&nbsp;Parse_Date $1 117&nbsp;check_date $day $month $year       #  See if valid date. 118&nbsp; 119&nbsp;strip_leading_zero $day            #  Remove any leading zeroes 120&nbsp;day=$?                             #+ on day and/or month. 121&nbsp;strip_leading_zero $month 122&nbsp;month=$? 123&nbsp; 124&nbsp;let "date1 = `day_index $day $month $year`" 125&nbsp; 126&nbsp; 127&nbsp;Parse_Date $2 128&nbsp;check_date $day $month $year 129&nbsp; 130&nbsp;strip_leading_zero $day 131&nbsp;day=$? 132&nbsp;strip_leading_zero $month 133&nbsp;month=$? 134&nbsp; 135&nbsp;date2=$(day_index $day $month $year) # Command substitution. 136&nbsp; 137&nbsp; 138&nbsp;calculate_difference $date1 $date2 139&nbsp; 140&nbsp;abs $diff                            # Make sure it's positive. 141&nbsp;diff=$value 142&nbsp; 143&nbsp;echo $diff 144&nbsp; 145&nbsp;exit 0 146&nbsp; 147&nbsp;#  Compare this script with 148&nbsp;#+ the implementation of Gauss' Formula in a C program at: 149&nbsp;#+    http://buschencrew.hypermart.net/software/datedif</PRE></TD></TR></TABLE><HR></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="MAKEDICT"></A><P><B>Example A-8. Making a <ICLASS="FIRSTTERM">dictionary</I></B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# makedict.sh  [make dictionary]   3&nbsp;   4&nbsp;# Modification of /usr/sbin/mkdict (/usr/sbin/cracklib-forman) script.   5&nbsp;# Original script copyright 1993, by Alec Muffett.   6&nbsp;#   7&nbsp;#  This modified script included in this document in a manner   8&nbsp;#+ consistent with the "LICENSE" document of the "Crack" package   9&nbsp;#+ that the original script is a part of.  10&nbsp;  11&nbsp;#  This script processes text files to produce a sorted list  12&nbsp;#+ of words found in the files.  13&nbsp;#  This may be useful for compiling dictionaries  14&nbsp;#+ and for other lexicographic purposes.  15&nbsp;  16&nbsp;  17&nbsp;E_BADARGS=65  18&nbsp;  19&nbsp;if [ ! -r "$1" ]                    #  Need at least one  20&nbsp;then                                #+ valid file argument.  21&nbsp;  echo "Usage: $0 files-to-process"  22&nbsp;  exit $E_BADARGS  23&nbsp;fi    24&nbsp;  25&nbsp;  26&nbsp;# SORT="sort"                       #  No longer necessary to define options  27&nbsp;                                    #+ to sort. Changed from original script.  28&nbsp;  29&nbsp;cat $* |                            # Contents of specified files to stdout.  30&nbsp;        tr A-Z a-z |                # Convert to lowercase.  31&nbsp;        tr ' ' '\012' |             # New: change spaces to newlines.  32&nbsp;#       tr -cd '\012[a-z][0-9]' |   #  Get rid of everything non-alphanumeric  33&nbsp;                                    #+ (in original script).  34&nbsp;        tr -c '\012a-z'  '\012' |   #  Rather than deleting non-alpha chars,  35&nbsp;                                    #+ change them to newlines.  36&nbsp;        sort |                      # $SORT options unnecessary now.  37&nbsp;        uniq |                      # Remove duplicates.  38&nbsp;        grep -v '^#' |              # Delete lines beginning with a hashmark.  39&nbsp;        grep -v '^$'                # Delete blank lines.  40&nbsp;  41&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><P><ANAME="SOUNDEX0"></A></P><DIVCLASS="EXAMPLE"><HR><ANAME="SOUNDEX"></A><P><B>Example A-9. Soundex conversion</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# soundex.sh: Calculate "soundex" code for names   3&nbsp;   4&nbsp;# =======================================================   5&nbsp;#        Soundex script   6&nbsp;#              by   7&nbsp;#         Mendel Cooper   8&nbsp;#     thegrendel@theriver.com   9&nbsp;#       23 January, 2002  10&nbsp;#  11&nbsp;#   Placed in the Public Domain.  12&nbsp;#  13&nbsp;# A slightly different version of this script appeared in  14&nbsp;#+ Ed Schaefer's July, 2002 "Shell Corner" column  15&nbsp;#+ in "Unix Review" on-line,  16&nbsp;#+ http://www.unixreview.com/documents/uni1026336632258/  17&nbsp;# =======================================================  18&nbsp;  19&nbsp;  20&nbsp;ARGCOUNT=1                     # Need name as argument.

⌨️ 快捷键说明

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