Unix 脚本编程总结与应用实例.doc_第1页
Unix 脚本编程总结与应用实例.doc_第2页
Unix 脚本编程总结与应用实例.doc_第3页
Unix 脚本编程总结与应用实例.doc_第4页
Unix 脚本编程总结与应用实例.doc_第5页
已阅读5页,还剩72页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

ea810a95fc9f0ed48dc5f057891f8360.pdfConfidentiality level 密级Unix 脚本编程总结与应用实例Catalog 目 录Revision Record 修订记录2Catalog 目 录3Title 标 题:Unix 脚本编程总结与应用实例8Keywords 关键词:8Abstract 摘 要:8List of abbreviations 缩略语清单:8命令格式9一shell与Unix平台9二shell基本知识9三grep 命令11四sed11五awk命令11六find命令12七test命令12八expr命令13九流程控制语法131. if-then-elif-else-fi语句134. for语句156. while语句167. until 语句178. break及continue189. case 语句18十shell脚本调试19实例讲解21一模式匹配211ls显示所有以hosts.开头的文件212ls显示包含x,y,z字符的所有文件21二正则表达式211grep匹配/etc/services文件中以ftp字符串开头的哪些文本行212grep匹配以system文本结尾的行。213grep匹配仅包含一个#字符的行。214grep匹配以或者abc开头的行215grep匹配以Ftp或者ftp开头的行216grep匹配F或者f以外的字符217grep匹配除大写字符以外的字符218grep匹配以ftp或telnet开头的文本行219grep匹配以ftp开头,后跟0个或多个-agent的文本行2110grep匹配以ftp开头,后跟1个或多个-agent的文本行2111grep匹配带有数字6,后跟至少3个0的文本行(使用-E启用边界特性)2212grep匹配含有(abc)的文本2213常用正则表达式举例2214grep精确匹配:在抽取字符串后加。2315grep消除大小写:加入-i选项2316特殊字符:$ . “ * | + ?2317grep判断变量含有HOST字符串2318grep判断变量含有xxx字符串2319grep匹配后缀为c,h,j,s,cpp,hpp的文件2320使用grep在文件中查找变量2321正则表达式语法23三sed命令271sed文本的定位方法:272sed编辑命令273sed打印文件的第二行274sed打印文件的第一到三行275sed打印匹配test的行286sed打印匹配$的行287sed打印最后一行:$是代表最后一行的特殊字符288sed脚本文件289sed上例中如果将a改为i:2810sed上例中如果将a改为c:2811sed删除第一行2812sed删除第一到第三行2813sed删除最后一行2814sed替换文本2815sed输出到文件,w选项:2816sed从文件读取,r选项:2817sed优势:2818sed常见的一行命令集2919sed去掉字串变量前后的空格2920sed去除文件count中的前后的空格2921sed提取最后一个目录名和程序名3022dirname或参数扩展提取目录名3023grep,sed获取文件的扩展名3124sed获取第n个匹配的行的行号31四awk命令311awk字段分隔符:312awk匹配模式323awk“模式匹配动作”324awk内部变量325awk用户定义变量326awk算术运算337awk高级算符338awk内部算术函数339awk内置函数3310awk的自定义函数3411awk的“下一”语句:3512awk中的字符串相加:3513awk的逻辑运算符3514awk的逻辑与|、逻辑或&3515awk的FS、OFS和ORS使用:3516awk的sprintf函数的使用3617awk的重定向,输出到文件:3618awk删除文件的第一行3619awk删除输入行中特定行的换行字符3620awk获取输入行中,域的最大个数3621awk输出一行超过80 个字符的每一行3622awk输出至少一个域的所有行。可用来将一个文档里的所有空白行删除3623awk输出范围在0 到100 之间的7 个随机数3624awk将所有用户的login名称依照字母的顺序输出3625awk将一个文档的总行数输出3626awk输出文档的内容时会在每行的最前面输出行号它的功能与cat -n 类似3627awk自定义函数的例子一: 第一个域与第二个域的平方和3728awk的split、数组、注释3729awk打印传入字符串的第n个分隔域。3730在awk中使用shell中的变量3731在某个目录下查找包含特定字符串的文件。返回文件名称38五其它命令381eval命令:可用于动态生成和执行代码382exitn:退出393export导出环境变量:394shift命令:395shell的参数扩展:396$PWD当前工作目录的名称$TERM用户的控制终端的类型.$LINENO所在的代码行,一般用来输出错误行号shift n将命令行参数往左移n位,但$0不变export变量名表将变量名表所列变量传递给子进程read 变量名表从标准输入读字符串,传给指定变量echo变量名表将变量名表指定的变量显示到标准输出set显示设置变量env显示目前所有变量set命令可以重新设定参数表.如set hello wold命令会设定$*为字符串hello world,$n和$#也同时受影响。shift命令可以将所有参数左移一个单位,$*、$n、$#均受影响。数组(在sh中不支持,可以在ksh中使用):$#varlist数组元素个数$datalistindex数组元素$#datalistindex数组元素长度执行命令:1) command :直接执行命令command2) sh command:启动一个shell process执行命令command3) . command:在本process中执行命令command4) exec command:本Script将会被所执行的命令所取代,当这个命令执行完毕之後,本Script也会随之结束。echo 命令使用的特殊字符b退格c显示新行,但是不把光标移到下一行f换页n换行(光标移到下一行)r回车t水平制表符v垂直制表符反斜杠ONASCII码为八进制数N的字符set命令: 把各位置参数的值依次设为”argument-list”里指定的参数,即重新设置$*,$,$1-$9如: set “hello” “world”则 $*和$都变成 hello world$1为hello$2为world三grep 命令搜索文本的匹配内容。格式:grep -option pattern filename选项:-c只输出匹配行的计数-i不区分大小写(只适用于单字符)-h查询多文件时不显示文件名-l查询多文件时只输出包含匹配字符的文件名-n显示匹配行及行号-s不显示不存在或无匹配文本的错误信息-v显示不包括匹配文本的所有行四sed查找和编辑文本。格式: (1) 直接键入命令sed -option command_line filename(2) 将sed命令插入脚本文件,然后调用sedsed -option -f program_file filename(3) 将sed命令插入脚本文件,并使脚本可执行sed program_file -option filename选项:n 不打印;sed不把编辑行写到标准输出,默认为打印所有行(编辑的和未编辑的)。p命令可以用来打印编辑行。e 下一个命令是编辑命令。在使用多项编辑时要加入该选项。f 如果正在调用sed脚本,要使用此选项。此选项sed脚本支持所有的sed命令。五awk命令awk 是一种程序语言,对于资料的处理具有很强的功能,对于文档里的资料做修改、比较、抽取等处理,awk 能够以很短的程序轻易地完成。如果使用C 语言写程序完成上述的操作不方便且很花费时间,所写的程序也会很大。awk 能够依照用户定义的格式来分解输入的资料也可以依照用户定义的格式来打印资料。awk 可用于在对象文件中逐行读取记录,按照命令中定义的匹配模式寻找相关记录,然后对该记录进行操作动作。格式:(1)直接键入命令:awk -Fchar command_line filename(2)将awk命令插入脚本文件,然后调用awk:awk -f program_file filename前一种形式的-Fchar确定间隔符,command_line为操作动作,filename为对象文件。后一种形式的program_file是指用户按一定格式编制好的对对象文件的匹配与操作。六find命令通过文件名或其它特征查找文件。格式:find path-list predicate-list选项:-typetp文件类型为tp:b块特别文件c字符设备特别文件d目录文件f普通文件p管道文件(FIFO)ssocketI符号链接文件-user uname文件属于用户uname。-group gname文件属于组gname。-sizen文件是n块大小(每块512字节),若n后跟一个c,单位为字节。-atimen在n天内已访问过此文件。-mtime n在n天内已修改过此文件-ctimen在n天内文件被修改、属性(拥有者、组、链接数等)被修改。-execcommand ; 执行命令-print打印当前路径名-newerfile修改时间比file文件晚七test命令命令格式test expression expression中包含一个以上的判断准则以作为test评诂的标准。两准则间用-a代 表逻辑AND 运算,-o代表逻辑OR运算,而在准则前放置一! 代表NOT 运算。如 果没有括号,则优先权则为! -a -o 。和expr命令相同,相使用左右括 号时,必须在其前面加上 。以下是有关准则的叙述(符合叙述时传回真,否则传回伪):string string不为空白字串 -n string string的长度大於0-z string string的长度等於0string1=string2 string1等於string2 string1!=string2 string1不等於string2 int1 -gt int2 int1大於int2 int1 -ge int2 int1大於等於int2int1 -eq int2 int1等於int2int1 -ne int2 int1不等於int2 int1 -le int2 int1小於等於int2int1 -lt int2 int1小於int2-r filename 档案可读取 -w filename 档案可写入 -x filename 档案可执行-f filename 档案为一般档-d filename 档案为目录 -s filename 档案为非空的一般档 test -r $filename -a -s $filename八expr命令命令格式 expr expressionexpression是由字串以及运算子所组成,每个字串或是运算子之间必须用空白隔开 。下表是运算子的种类及功能,而优先顺序则以先後次序排列,可以利用小括号来改变运算的优先次序。其运算结果则输出至标准输出上。: 字串比较。比较的方式是以两字串的第一个字母开始,而以第二个字串的 字母结束。如果相同时,则输出第二个字串的字母个数,如果不同时则传 回0 。|OR运算,如果它非null或者非0,返回第一个表达式,否则返回第二个表达式&AND运算,如果非null或非0,返回第一个表达式,否则返回0=、=、!=整数比较运算符+、-、*、/、%整数算术运算符,其中%求余数。当expression中含有*, (, ) 等符号时,必须在其前面加上 ,以免被 Shell 解释成其它意义。 expr 2 * ( 3 + 4 ) 其输出为14 九流程控制语法1. if-then-elif-else-fi语句 if expressionthenelif expressionthenthen-command-list. . .elseelse-command-listfiif-then-fi语句的语义if-then-else-fi语句的语义if-then-elif-else-fi语句的语义4. for语句 for variable in argument-list do command-list done for语句的语义for var do commands done 例如for argdoecho $vardone调用 varlist.sh 111 222 3333输出: 111 222 3336. while语句while expressiondo command-list done while语句的语义7. until 语句 until expressiondo command-list done until语句的语义8. break及continue 这两者是用於for, while, until 等循环控制下。break 会跳至done后方执行 ,而continue会跳至done执行,继续执行循环。9. case 语句 case test-string inpat1) command-list1; pat2) command-list2; . . .patN)command-listN;*);esac case语句的语义而pat 除了可以指定一些确定的字串,也可以指定字串的集合,如下* 任意字串 ? 任意字元 abc a, b, 或c三字元其中之一 a-n 从a到n的任一字元| 多重选择,如A|a 十shell脚本调试1.启动调试启动调试Shell脚本的基本语法为: $/bin/sh option script arg1 arg2 . argN这里显式声明了要执行脚本的Shell为/bin/sh,script是脚本的名字,arg1到argN是脚本的参数, option为调试选项,如下所示: -n读所有的命令,但不执行它们 -v在读时显示所有的行-x在执行时显示所有命令和它们的参数。该选项常称为shell 跟踪选项或改变脚本的第一行,象下面那样在该行声明一个调试选项:#!/bin/sh option2.使用set命令在每个调用激活调试模式中,调试模式的缺省行为对脚本中从第一行到最后一行都有效。有时我们只需要调试特定的函数或脚本的一部分,这时调试整个脚本就有些多余。通过使用set命令,我们可以在shell脚本的任何地方启动或取消调试,其基本语法为:set -|+ option这里的option选项与上面的相同。set x 回显set v 详细如#!/bin/ksh -xv3.语法检查在处理任何Shell脚本时,应在准备执行它之前检查脚本的语法,这使我们能改正许多问题。要启动语法检查可使用-n选项,如对于上面的buggy.sh脚本,象下面那样检查语法:$/bin/sh -n ./buggy.sh实例讲解一模式匹配1ls显示所有以hosts.开头的文件lslhosts.* 2ls显示包含x,y,z字符的所有文件lsd*x-z*二正则表达式1grep匹配/etc/services文件中以ftp字符串开头的哪些文本行grepftp/etc/services2grep匹配以system文本结尾的行。grepsystem$file3grep匹配仅包含一个#字符的行。grep#$file4grep匹配以或者abc开头的行grepfile5grep匹配以Ftp或者ftp开头的行grepFftpfile6grep匹配F或者f以外的字符grepFffile7grep匹配除大写字符以外的字符grepA-Zfile8grep匹配以ftp或telnet开头的文本行grepftp|telnetfile9grep匹配以ftp开头,后跟0个或多个-agent的文本行grepftp(-agent)?/etc/services或grepftp(-agent)*/etc/services注:a)在sco unix下,上面的单括号前要加转义符b)在sun os 5.8下,不论加不加单括号均不支持。10grep匹配以ftp开头,后跟1个或多个-agent的文本行grepftp(-agent)+/etc/services说明同上。11grep匹配带有数字6,后跟至少3个0的文本行(使用-E启用边界特性)grep-E603,/etc/services12grep匹配含有(abc)的文本grep(abc)file13常用正则表达式举例正则表达式匹配功能the以the开头行SsignalL匹配单词signal,signaL,Signal,SignaLSsignalL.同上,但加一个句点mayMAYUSER$只包含USER的行tty$以tty结尾的行.带句点的行d.x.x.x对用户、用户组及其它用户组成员有可执行权限的目录l排除关联目录的目录列表.*00之前或之后加任意字符000*000或更多个iI大写或小写IiInN大写或小写I或n$空行.*$匹配行中任意字符$包括6各字符的行a-zA-Z任意单字符a-za-z*至少两个小写字母0-9$非数字或美元表示0-9A-Za-z非数字或字母1231到3中的一个数字Ddevice单词Device或deviceDe.ce前两个字母为De,后跟两个任意字符,最后为ce.$仅有一个字符的行.0-90-9以一个句点和两个数字开始的行“Device”单词DeviceDeVvice.单词Device.或DeVice.0-92-0-92-0-94日期格式dd-mm-yyyy1-90-91,2.0-91,3.0-91,3.0-91,3IP地址格式nnn.nnn.nnn.nnn.*$匹配任意行14grep精确匹配:在抽取字符串后加。grep “48” file15grep消除大小写:加入-i选项grep -I “sept” file16特殊字符:$ . “ * | + ?如果要查询这些字符,需要在前面加转义字符。17grep判断变量含有HOST字符串if 1 -eq echo $VarName | grep -c HOST ; then18grep判断变量含有xxx字符串if 1 -eq echo $VarName | grep -c .* ; then19grep匹配后缀为c,h,j,s,cpp,hpp的文件EXT_ALL=chjsEXT_PP=chEXT_NO_PP=jsls $1 | grep .$EXT_ALLp0,2$ | grep -v .$ EXT_NO_PPp1,2$ | grep -v .$ EXT_PPp1$20使用grep在文件中查找变量grep echo $user /etc/passwd | cut f5 d:21正则表达式语法来自msdn,仅供参考。Here are some examples of regular expressions:ExpressionMatches/s*$/Match a blank line./d2-d5/Validate an ID number consisting of 2 digits, a hyphen, and an additional 5 digits./*)?sS*/Match an HTML tag.The following table contains the complete list of metacharacters and their behavior in the context of regular expressions:CharacterDescriptionMarks the next character as a special character, a literal, a backreference, or an octal escape. For example, n matches the character n. n matches a newline character. The sequence matches and ( matches (.Matches the position at the beginning of the input string. If the RegExp objects Multiline property is set, also matches the position following n or r.$Matches the position at the end of the input string. If the RegExp objects Multiline property is set, $ also matches the position preceding n or r.*Matches the preceding character or subexpression zero or more times. For example, zo* matches z and zoo. * is equivalent to 0,.+Matches the preceding character or subexpression one or more times. For example, zo+ matches zo and zoo, but not z. + is equivalent to 1,.?Matches the preceding character or subexpression zero or one time. For example, do(es)? matches the do in do or does. ? is equivalent to 0,1nn is a nonnegative integer. Matches exactly n times. For example, o2 does not match the o in Bob, but matches the two os in food.n,n is a nonnegative integer. Matches at least n times. For example, o2, does not match the o in Bob and matches all the os in foooood. o1, is equivalent to o+. o0, is equivalent to o*.n,mM and n are nonnegative integers, where n = m. Matches at least n and at most m times. For example, o1,3 matches the first three os in fooooood. o0,1 is equivalent to o?. Note that you cannot put a space between the comma and the numbers.?When this character immediately follows any of the other quantifiers (*, +, ?, n, n, n,m), the matching pattern is non-greedy. A non-greedy pattern matches as little of the searched string as possible, whereas the default greedy pattern matches as much of the searched string as possible. For example, in the string oooo, o+? matches a single o, while o+ matches all os.Matches any single character except n. To match any character including the n, use a pattern such as sS.(pattern)A subexpression that matches pattern and captures the match. The captured match can be retrieved from the resulting Matches collection using the $0$9 properties. To match parentheses characters ( ), use ( or ).(?:pattern)A subexpression that matches pattern but does not capture the match, that is, it is a non-capturing match that is not stored for possible later use. This is useful for combining parts of a pattern with the or character (|). For example, industr(?:y|ies) is a more economical expression than industry|industries.(?=pattern)A subexpression that performs a positive lookahead search, which matches the string at any point where a string matching pattern begins. This is a non-capturing match, that is, the match is not captured for possible later use. For example Windows (?=95|98|NT|2000) matches Windows in Windows 2000 but not Windows in Windows 3.1. Lookaheads do not consume characters, that is, after a match occurs, the search for the next match begins immediately following the last match, not after the characters that comprised the lookahead.(?!pattern)A subexpression that performs a negative lookahead search, which matches the search string at any point where a string not matching pattern begins. This is a non-capturing match, that is, the match is not captured for possible later use. For example Windows (?!95|98|NT|2000) matches Windows in Windows 3.1 but does not match Windows in Windows 2000. Lookaheads do not consume characters, that is, after a match occurs, the search for the next match begins immediately following the last match, not after the characters that comprised the lookahead.x|yMatches either x or y. For example, z|food matches z or food. (z|f)ood matches zood or food. xyzA character set. Matches any one of the enclosed characters. For example, abc matches the a in plain. xyzA negative character set. Matches any character not enclosed. For example, abc matches the p in plain. a-zA range of characters. Matches any character in the specified range. For example, a-z matches any lowercase alphabetic character in the range a through z. a-zA negative range characters. Matches any character not in the specified range. For example, a-z matches any character not in the range a through z. bMatches a word boundary, that is, the position between a word and a space. For example, erb matches the er in never but not the er in verb. BMatches a nonword boundary. erB matches the er in verb but not the er in never. cxMatches the control character indicated

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论