contributed-scripts.html
来自「BASH Shell 编程 经典教程 《高级SHELL脚本编程》中文版」· HTML 代码 · 共 2,144 行 · 第 1/5 页
HTML
2,144 行
6 7 HERE=`uname -n` # ==> hostname 8 THERE=bilbo 9 echo "starting remote backup to $THERE at `date +%r`" 10 # ==> `date +%r` returns time in 12-hour format, i.e. "08:08:34 PM". 11 12 # make sure /pipe really is a pipe and not a plain file 13 rm -rf /pipe 14 mkfifo /pipe # ==> Create a "named pipe", named "/pipe". 15 16 # ==> 'su xyz' runs commands as user "xyz". 17 # ==> 'ssh' invokes secure shell (remote login client). 18 su xyz -c "ssh $THERE \"cat >/home/xyz/backup/${HERE}-daily.tar.gz\" < /pipe"& 19 cd / 20 tar -czf - bin boot dev etc home info lib man root sbin share usr var >/pipe 21 # ==> Uses named pipe, /pipe, to communicate between processes: 22 # ==> 'tar/gzip' writes to /pipe and 'ssh' reads from /pipe. 23 24 # ==> The end result is this backs up the main directories, from / on down. 25 26 # ==> What are the advantages of a "named pipe" in this situation, 27 # ==>+ as opposed to an "anonymous pipe", with |? 28 # ==> Will an anonymous pipe even work here? 29 30 31 exit 0</PRE></FONT></TD></TR></TABLE><HR></DIV><P>+</P><P>Stephane Chazelas捐献了这个脚本, 用来展示如何不使用数组来产生素数</P><DIVCLASS="EXAMPLE"><HR><ANAME="PRIMES"></A><P><B>例子 A-16. 使用模操作符来产生素数</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><FONTCOLOR="#000000"><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 # primes.sh: Generate prime numbers, without using arrays. 3 # Script contributed by Stephane Chazelas. 4 5 # This does *not* use the classic "Sieve of Eratosthenes" algorithm, 6 #+ but instead uses the more intuitive method of testing each candidate number 7 #+ for factors (divisors), using the "%" modulo operator. 8 9 10 LIMIT=1000 # Primes 2 - 1000 11 12 Primes() 13 { 14 (( n = $1 + 1 )) # Bump to next integer. 15 shift # Next parameter in list. 16 # echo "_n=$n i=$i_" 17 18 if (( n == LIMIT )) 19 then echo $* 20 return 21 fi 22 23 for i; do # "i" gets set to "@", previous values of $n. 24 # echo "-n=$n i=$i-" 25 (( i * i > n )) && break # Optimization. 26 (( n % i )) && continue # Sift out non-primes using modulo operator. 27 Primes $n $@ # Recursion inside loop. 28 return 29 done 30 31 Primes $n $@ $n # Recursion outside loop. 32 # Successively accumulate positional parameters. 33 # "$@" is the accumulating list of primes. 34 } 35 36 Primes 1 37 38 exit 0 39 40 # Uncomment lines 16 and 24 to help figure out what is going on. 41 42 # Compare the speed of this algorithm for generating primes 43 #+ with the Sieve of Eratosthenes (ex68.sh). 44 45 # Exercise: Rewrite this script without recursion, for faster execution.</PRE></FONT></TD></TR></TABLE><HR></DIV><P>+</P><P>这是Jordi Sanfeliu的<EM>tree</EM>脚本的升级版, 由Rick Boivie编写. </P><DIVCLASS="EXAMPLE"><HR><ANAME="TREE"></A><P><B>例子 A-17. <BCLASS="COMMAND">tree</B>: 显示目录树</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><FONTCOLOR="#000000"><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 # tree.sh 3 4 # Written by Rick Boivie. 5 # Used with permission. 6 # This is a revised and simplified version of a script 7 #+ by Jordi Sanfeliu (and patched by Ian Kjos). 8 # This script replaces the earlier version used in 9 #+ previous releases of the Advanced Bash Scripting Guide. 10 11 # ==> Comments added by the author of this document. 12 13 14 search () { 15 for dir in `echo *` 16 # ==> `echo *` lists all the files in current working directory, 17 #+ ==> without line breaks. 18 # ==> Similar effect to for dir in * 19 # ==> but "dir in `echo *`" will not handle filenames with blanks. 20 do 21 if [ -d "$dir" ] ; then # ==> If it is a directory (-d)... 22 zz=0 # ==> Temp variable, keeping track of directory level. 23 while [ $zz != $1 ] # Keep track of inner nested loop. 24 do 25 echo -n "| " # ==> Display vertical connector symbol, 26 # ==> with 2 spaces & no line feed in order to indent. 27 zz=`expr $zz + 1` # ==> Increment zz. 28 done 29 30 if [ -L "$dir" ] ; then # ==> If directory is a symbolic link... 31 echo "+---$dir" `ls -l $dir | sed 's/^.*'$dir' //'` 32 # ==> Display horiz. connector and list directory name, but... 33 # ==> delete date/time part of long listing. 34 else 35 echo "+---$dir" # ==> Display horizontal connector symbol... 36 # ==> and print directory name. 37 numdirs=`expr $numdirs + 1` # ==> Increment directory count. 38 if cd "$dir" ; then # ==> If can move to subdirectory... 39 search `expr $1 + 1` # with recursion ;-) 40 # ==> Function calls itself. 41 cd .. 42 fi 43 fi 44 fi 45 done 46 } 47 48 if [ $# != 0 ] ; then 49 cd $1 # move to indicated directory. 50 #else # stay in current directory 51 fi 52 53 echo "Initial directory = `pwd`" 54 numdirs=0 55 56 search 0 57 echo "Total directories = $numdirs" 58 59 exit 0</PRE></FONT></TD></TR></TABLE><HR></DIV><P>经过Noah Friedman的授权, 他的<EM>string function</EM>脚本可以在本书中使用, 这个脚本本质上就是复制了一些C库的字符串操作函数. </P><DIVCLASS="EXAMPLE"><HR><ANAME="STRING"></A><P><B>例子 A-18. <BCLASS="COMMAND">string functions</B>: C风格的字符串函数</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><FONTCOLOR="#000000"><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 3 # string.bash --- bash emulation of string(3) library routines 4 # Author: Noah Friedman <friedman@prep.ai.mit.edu> 5 # ==> Used with his kind permission in this document. 6 # Created: 1992-07-01 7 # Last modified: 1993-09-29 8 # Public domain 9 10 # Conversion to bash v2 syntax done by Chet Ramey 11 12 # Commentary: 13 # Code: 14 15 #:docstring strcat: 16 # Usage: strcat s1 s2 17 # 18 # Strcat appends the value of variable s2 to variable s1. 19 # 20 # Example: 21 # a="foo" 22 # b="bar" 23 # strcat a b 24 # echo $a 25 # => foobar 26 # 27 #:end docstring: 28 29 ###;;;autoload ==> Autoloading of function commented out. 30 function strcat () 31 { 32 local s1_val s2_val 33 34 s1_val=${!1} # indirect variable expansion 35 s2_val=${!2} 36 eval "$1"=\'"${s1_val}${s2_val}"\' 37 # ==> eval $1='${s1_val}${s2_val}' avoids problems, 38 # ==> if one of the variables contains a single quote. 39 } 40 41 #:docstring strncat: 42 # Usage: strncat s1 s2 $n 43 # 44 # Line strcat, but strncat appends a maximum of n characters from the value 45 # of variable s2. It copies fewer if the value of variabl s2 is shorter 46 # than n characters. Echoes result on stdout. 47 # 48 # Example: 49 # a=foo 50 # b=barbaz 51 # strncat a b 3 52 # echo $a 53 # => foobar 54 # 55 #:end docstring: 56 57 ###;;;autoload 58 function strncat () 59 { 60 local s1="$1" 61 local s2="$2" 62 local -i n="$3" 63 local s1_val s2_val 64 65 s1_val=${!s1} # ==> indirect variable expansion 66 s2_val=${!s2} 67 68 if [ ${#s2_val} -gt ${n} ]; then 69 s2_val=${s2_val:0:$n} # ==> substring extraction 70 fi 71 72 eval "$s1"=\'"${s1_val}${s2_val}"\' 73 # ==> eval $1='${s1_val}${s2_val}' avoids problems, 74 # ==> if one of the variables contains a single quote. 75 } 76 77 #:docstring strcmp: 78 # Usage: strcmp $s1 $s2 79 # 80 # Strcmp compares its arguments and returns an integer less than, equal to, 81 # or greater than zero, depending on whether string s1 is lexicographically 82 # less than, equal to, or greater than string s2. 83 #:end docstring: 84 85 ###;;;autoload 86 function strcmp () 87 { 88 [ "$1" = "$2" ] && return 0 89 90 [ "${1}" '<' "${2}" ] > /dev/null && return -1 91 92 return 1 93 } 94 95 #:docstring strncmp: 96 # Usage: strncmp $s1 $s2 $n 97 # 98 # Like strcmp, but makes the comparison by examining a maximum of n 99 # characters (n less than or equal to zero yields equality).100 #:end docstring:101 102 ###;;;autoload103 function strncmp ()104 {105 if [ -z "${3}" -o "${3}" -le "0" ]; then106 return 0107 fi108 109 if [ ${3} -ge ${#1} -a ${3} -ge ${#2} ]; then110 strcmp "$1" "$2"111 return $?112 else113 s1=${1:0:$3}114 s2=${2:0:$3}115 strcmp $s1 $s2116 return $?117 fi118 }119 120 #:docstring strlen:121 # Usage: strlen s122 #123 # Strlen returns the number of characters in string literal s.124 #:end docstring:125 126 ###;;;autoload127 function strlen ()128 {129 eval echo "\${#${1}}"130 # ==> Returns the length of the value of the variable131 # ==> whose name is passed as an argument.132 }133 134 #:docstring strspn:135 # Usage: strspn $s1 $s2136 # 137 # Strspn returns the length of the maximum initial segment of string s1,138 # which consists entirely of characters from string s2.139 #:end docstring:140 141 ###;;;autoload142 function strspn ()143 {144 # Unsetting IFS allows whitespace to be handled as normal chars. 145 local IFS=146 local result="${1%%[!${2}]*}"147 148 echo ${#result}149 }150 151 #:docstring strcspn:152 # Usage: strcspn $s1 $s2153 #154 # Strcspn returns the length of the maximum initial segment of string s1,155 # which consists entirely of characters not from string s2.156 #:end docstring:157 158 ###;;;autoload159 function strcspn ()160 {161 # Unsetting IFS allows whitspace to be handled as normal chars. 162 local IFS=163 local result="${1%%[${2}]*}"164 165 echo ${#result}166 }167 168 #:docstring strstr:169 # Usage: strstr s1 s2170 # 171 # Strstr echoes a substring starting at the first occurrence of string s2 in172 # string s1, or nothing if s2 does not occur in the string. If s2 points to173 # a string of zero length, strstr echoes s1.174 #:end docstring:175 176 ###;;;autoload177 function strstr ()178 {179 # if s2 points to a string of zero length, strstr echoes s1180 [ ${#2} -eq 0 ] && { echo "$1" ; return 0; }181 182 # strstr echoes nothing if s2 does not occur in s1183 case "$1" in184 *$2*) ;;185 *) return 1;;186 esac187 188 # use the pattern matching code to strip off the match and everything189 # following it190 first=${1/$2*/}191 192 # then strip off the first unmatched portion of the string193 echo "${1##$first}"194 }195 196 #:docstring strtok:197 # Usage: strtok s1 s2198 #199 # Strtok considers the string s1 to consist of a sequence of zero or more200 # text tokens separated by spans of one or more characters from the201 # separator string s2. The first call (with a non-empty string s1202 # specified) echoes a string consisting of the first token on stdout. The203 
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?