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