10-2shell编程.ppt_第1页
10-2shell编程.ppt_第2页
10-2shell编程.ppt_第3页
10-2shell编程.ppt_第4页
10-2shell编程.ppt_第5页
已阅读5页,还剩53页未读 继续免费阅读

下载本文档

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

文档简介

LinuxShell编程 shell程序设计的流程控制 和其他高级程序设计语言一样 shell提供了用来控制程序执行流程的命令 包括条件分支和循环结构 用户可以用这些命令建立非常复杂的程序 与传统的语言不同的是 shell用于指定条件值的不是布尔表达式而是命令和字符串 test测试命令 test命令用于检查某个条件是否成立 它可以进行数值 字符和文件三个方面的测试 其测试符和相应的功能分别如下 1 数值测试 eq 等于则为真 ne 不等于则为真 gt 大于则为真 ge 大于等于则为真 lt 小于则为真 le 小于等于则为真 2 字符串测试 等于则为真 不相等则为真 z字符串 字符串长度伪则为真 n字符串 字符串长度不伪则为真 3 文件测试 e文件名 如果文件存在则为真 r文件名 如果文件存在且可读则为真 w文件名 如果文件存在且可写则为真 x文件名 如果文件存在且可执行则为真 s文件名 如果文件存在且至少有一个字符则为真 d文件名 如果文件存在且为目录则为真 f文件名 如果文件存在且为普通文件则为真 c文件名 如果文件存在且为字符型特殊文件则为真 b文件名 如果文件存在且为块特殊文件则为真 另外 Linux还提供了与 或 o 非 a 三个逻辑操作符用于将测试条件连接起来 其优先级为 最高 a 次之 o 最低 example test3 gt4echo 测试HOME变量的值 用单目操作符 d检查它是不是目录 test abc def echo abc def echo abc def echo abc abc echo if条件语句 shell程序中的条件分支是通过if条件语句来实现的 其一般格式为 if条件命令串then条件为真时的命令串else条件为假时的命令串fi if if then elif then else fielif和else是可选项 例子 bin bashnum1 10num2 11if num1 gt5 thenechothetestvalue num1isgreaterthan5fiif num2 eq num1 thenechothevaluesareequalelseechothevaluesaredifferentfi bin bashtestuser rootif USER testuser thenechothisisnot testuserelseechowelcome testuserfi bin bashstr1 aaastr2 bbbif str1 str2 thenecho str1isgreaterthan str2elseecho str1islessthan str2fi在脚本中单独使用了大于号 虽然没报错 但结果是错误的 bin bashstr1 aaastr2 bbbif str1 str2 thenecho str1isgreaterthan str2elseecho str1islessthan str2fi bin bashstr1 aaastr2 if n str1 长度是否大于0thenechothestring str1isnotemptyelseechothestring str1isemptyfiif z str2 长度是否为0thenechothestring str2isemptyelseechothestring str2isnotemptyfiif z str3 长度是否为0thenechothestring str3isemptyelseechothestring str3isnotemptyfi 使用 f确定对象是文件 bin bashif e HOME thenechothe HOMEexists if f HOME thenechoyes itisafileelseechono itisnotafileif f HOME bash history thenechobut HOME bash historyisafileelseechoitisnotafiletoofifielseechothereisnotobject fi d确定是否目录 bin bashif d HOME thenechoyourhomedirectoryexistscd HOMElselseechothereissometingwrongfi e检测对象是否存在 bin bashif e HOME thenechotheHDexistsif e HOME myfile thenechothemyfileexistselsetouch HOME myfileechocreatingnewfilenamemyfilefifi 通过 r可检测可读性 bin bashtestfile etc shadowif f testfile thenif r testfile thenls l testfileelseecho i munabletoreadthefile fielseecho thefiledoesn texist fi 通过 s检测文件是否为空 bin bashfile testfiletouch fileif s file thenechothefileexistsandhasdatainitelseechothefileexistsbutemptyfidate fileif s file thenechothefileexistsandhasdatainitelseechothefileexistsbutemptyfi 通过 w检测文件是否可写 bin bashfile HOME testfiletouch filechmodu w filenow date if w file thenechothefilecouldbewrittenelseecho thefilecouldn tbewritten fichmodu w fileif w file thenechothefilecouldbewritten now HOME testfileechoandthefileviews fileelseecho thefilecouldn tbewritten fi 通过 x可以检测文件是否可被执行 bin bashfile HOME testfile2touch filechmodu x fileif x file thenecho thefilecouldberun elseecho thefilecouldn tberun fi 通过 O可以检测文件的所有者 bin bashfile etc passwdif O file thenecho youaretheownerofthe file elseecho youaren ttheownerofthe file fi G检测文件的默认组 bin bashfile etc passwdif G file thenecho youareinthesamegroupas file elseecho youaren tinthesamegroupasthe file fi 通过 nt和 ot来比较两个文件之间的新旧 这里指的是创建或修改日期 bin bashif HOME aa nt HOME bb thenechotheaaisnewerthanbbelseechotheaaisolderthanbbfi 复合条件检测 bin bashif d HOME w HOME thenechothefileexistsandyoucanwirte HOMEelseecho youcan twritethefile fiif d HOME w HOME thenechothefileexistsandyoucanwirte HOMEelseecho youcan twritethefile fi 双圆括号 表示数学表达式 bin bashnum1 10if num1 2 90 then num2 num1 2 echothesquareof num1is num2fi 双方括号 表示高级字符串处理函数 使用双方括号可以定义与字符串值相匹配的正则表达式 bin bashif USER s thenechohello USERelseechosorry i don t knowyoufi case条件选择 格式如下 casestringinexp 1 若干个命令行1 exp 2 若干个命令行2 其他命令行esac shell通过计算字符串string的值 将其结果依次和表达式exp 1 exp 2等进行比较 直到找到一个匹配的表达式为止 如果找到了匹配项则执行它下面的命令直到遇到一对分号 为止 在case表达式中也可以使用shell的通配符 通常用 作为case命令的最后表达式以便使在前面找不到任何相应的匹配项时执行 其他命令行 的命令 bin bashcase USERinroot testuser echowelcome USERechoyourareadmin linuxidc echowelcome USER echowelcome USER esac for循环 for循环对一个变量的可能的值都执行一个命令序列 赋给变量的几个数值既可以在程序内以数值列表的形式提供 也可以在程序以外以位置参数的形式提供 for循环的一般格式为 for变量名 in数值列表 do若干个命令行done 变量名可以是用户选择的任何字符串 如果变量名是var 则在in之后给出的数值将顺序替换循环命令列表中的 var 如果省略了in 则变量var的取值将是位置参数 对变量的每一个可能的赋值都将执行do和done之间的命令列表 sgf localhostsgf cattest3 bin bashfortestinCCNACCNPRHCEOCPOCMdoechothelearninglistis testdone sgf localhostsgf test3thelearninglistisCCNAthelearninglistisCCNPthelearninglistisRHCEthelearninglistisOCPthelearninglistisOCM bin bashforcitiesinguangzhoushanghaibeijingdoechomyfavoritecityis citiesdone bin bashforcitiesin guangzhou shanghai beijing doechomyfavoritecityis citiesdone catsomefileaabbccdd cattest3 bin bashfile somefileforwordsin cat file doechothewordis wordsdone执行后调用了somefile文件的每一行 注意somefile所处的位置是shell脚本中可以调用的 不然必须使用绝对路径或相对路径了 test3thewordisaathewordisbbthewordisccthewordisdd 使用通配符读取目录 pwd home sgf test touchaabbcc mkdirddeeff先创建三个文件和三个目录 cattest3 bin bashforfilein HOME test doif d file thenecho fileisadirectory elif f file thenecho fileisafile fidone test3 home sgf test aaisafile home sgf test bbisafile home sgf test ccisafile home sgf test ddisadirectory home sgf test eeisadirectory home sgf test ffisadirectory C语言式的for命令 bin bashfor i 1 i 10 i doechothenextnumis idone while和until循环 while和until命令都是用命令的返回状态值来控制循环的 While循环的一般格式为 while若干个命令行1do若干个命令行2done bin bashvar 8while var gt0 doecho varvar var 1 done until 格式如下 until若干个命令行1do若干个命令行2doneuntil循环和while循环的区别在于 while循环在条件为真时继续执行循环 而until则是在条件为假时继续执行循环 bin bashvar 24until var eq0 doecho varvar var 8 done 嵌套循环 bin bashfor a 1 a 3 a doechooutsideloopis a for b 1 b 3 b doechoinsideloopis b donedone bin bashvar1 5while var1 ge0 doecho outerloopis var1 for var2 1 var2 3 var2 doecho innerloop var2 donevar1 var1 1 done 无条件控制语句break

温馨提示

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

评论

0/150

提交评论