shell编程1.ppt

上传人:qwe****56 文档编号:80597571 上传时间:2023-03-23 格式:PPT 页数:56 大小:129.50KB
返回 下载 相关 举报
shell编程1.ppt_第1页
第1页 / 共56页
shell编程1.ppt_第2页
第2页 / 共56页
点击查看更多>>
资源描述

《shell编程1.ppt》由会员分享,可在线阅读,更多相关《shell编程1.ppt(56页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、shell编程shell编程o帮助用户完成特定的任务,提高维护系统的效率o配置应用程序o通过shell命令解释器解释执行shell脚本2shell 编程的基本过程o建立建立 shell 文件文件n包含任意多行操作系统命令或shell命令的文本文件o赋予赋予shell文件执行权限文件执行权限n用chmod命令修改权限o执行执行shell文件文件n直接在命令行上调用shell程序3shell程序设计实例1.建立建立shell文件文件(可用任何建立文本文件的方法可用任何建立文本文件的方法):nvi test.shwho|grep$1$1,$2,$3为参数为参数2.赋予执行权限赋予执行权限:(初始文本

2、文件无执行权限初始文本文件无执行权限)n$chmod 740 test.shn$ls-l test.shn-rwxr-1 zlm None 14 Nov 30 21:24 test.sh4shell程序设计实例3.执行该执行该shell程序程序n$test.sh student5ntest.sh:not found4.指定路径或修改环境变量指定路径或修改环境变量PATH后执行后执行shell程序程序n$./test.sh student5nstudent5 tty06 Feb 8 09:125一个简单的shell程序#!/bin/bash#a simple shell script examp

3、le#a functionfunction say_hello()echo enter your name,please.:read nameecho hello$name!echo programme starts here.say_helloecho programme ends.6一个简单的shell程序$chmod+x shell.sh$./shell.sh programme starts here.enter your name,please.:tomhello tom!programme ends.7变量的声明和使用obash是一种弱类型的脚本语言(对类型是一种弱类型的脚本语言(

4、对类型的要求不严格)的要求不严格)o变量值变量值oa=“hello”b=8o引用变量引用变量n通过在变量名前加$符号oecho“a is$a”8标点符号在变量中的使用o单引号和双引号单引号和双引号n单引号禁止变量替换,元字符保持其符号本身;而双引号允许元字符变量替换n例如:$a=“he is a student”$echo“She said:$a”She said:he is a student$b=The value of a is$a$echo$bThe value of a is$a9标点符号在变量中的使用o花括号花括号n将变量名和后面的字符串区分开n$变量名n例如:$c=There i

5、s a teach$echo“$cer reading room”reading room$echo“$cer reading room”There is a teacher reading room10标点符号在变量中的使用o反撇号反撇号n反使变量按命令来执行n例如:$a=date$echo$adate$b=date$echo$bWed Nov 1 16:28:19 Beijing 200611变量的声明和使用o$变量名引用变量的扩展用法:o声明:variable是变量名,value代表一个具体的值n$variable:-value:如果变量variable存在,则返回variable的值,

6、否则返回值valuen$variable:=value:如果变量variable存在,则返回variable的值,否则先将值value赋给变量variable,然后返回值value12变量的声明和使用n$variable:+value:o如果变量variable存在,则返回值value,否则返回空值n$variable:?value:o如果变量variable存在,则返回variable的值,否则将value送到标准错误输出显示并退出shell程序n$variable:offset:length:ooffset和length为整数数字,中括号代表可选部分。变量variable的第(offset

7、+1)个字符开始的长度为length的子串,如果中括号内的部分省略,则返回第(offset+1)个字符后所有子串13例子$var=hello$echo$var$title:-somebody!hello somebody!$echo$var$title:+somebody!hello!$echo$var$title:?title is null or empty!bash:title:title is null or empty$echo$var$title:=tom and jerry!hello tom and jerry!$echo$var$title:+”somebody”!hello

8、 somebody!$echo$var$title:8:5!hello jerry!14常用的系统变量p$0 当前shell程序的名字p$1$9 命令行上的第一到第九个参数p$#命令行上的参数个数p$*命令行上的所有参数p$命令行上的参数集合p$当前进程的进程标识号(PID)p$?上一条命令的退出状态p$!最后一个后台进程的进程标识号15例如:o$echo aa bb cc dd$oaa bb cc dd 2391o$cat file1 file2 file3 2 errlogo$echo$?o1 n(非0表示命令运行失败,错误信息在errlog 文件中)16shell 程序和语句oshell

9、 程序由零至多条shell语句构成,shell语句包括三大类:n说明性语句:说明性语句:o以#号开始到行尾的部分,不被解释执行n功能性语句功能性语句:o操作系统命令、shell内部命令、自编程序等n结构性语句:结构性语句:o条件测试语句、多路分支语句、循环语句、循环控制语句等17说明性语句o注释行从注释行从#号开始到所在行的行尾,其内容不被号开始到所在行的行尾,其内容不被解释执行解释执行,可以出现在程序中的任何位置可以出现在程序中的任何位置o可以单独占用一行可以单独占用一行,也可以接在执行语句的后面也可以接在执行语句的后面#!/bin/sh#command_2#command_2的语句说明的语

10、句说明#下面程序段的说明#command_m#command_n 18功能性语句oread命令nread从标准输入读入一行,并赋值给后面的变量n语法语法:oread varn把读入的数据全部赋给var oread var1 var2 var3n把读入行中的第一个词赋给var1,第二个词赋给var2,把其余所有的词赋给最后一个变量19read应用实例#example1 for readecho Input your name:nread usernameecho Your name is$username#example2 for readecho Input date with format

11、yyyy mm dd:nread year month dayecho Today is$year/$month/$day,right?echo Press any key to confirm and continuenread answerecho I know the date,bye!20功能性语句oexpr 命令命令n算术运算命令expr主要用于进行简单的整数运算,包括加(+)、减(-)、乘(*)、整除(/)和求模(%)等操作$expr 12 +5 *327$num=9$sum=expr$num *6$echo$sum5421结构性语句o结构性语句主要根据程序的运行状态、输入数据、变

12、量的取值、控制信号以及运行时间等因素来控制程序的运行流程o主要包括n条件判断语句n多路分支语句n循环语句n循环控制语句n后台执行语句22条件判断语句o对某个条件(文件属性、代码执行结果等)进行判断o判断结果:真(0),假(1)o条件判断格式:ntest condition或或 condition o使用判断时,括号要与判断条件之间用空格分隔开23条件判断语句o条件可以是如下几种情况n文件属性的测试:文件类型、文件访问权限等n字符串属性的测试:字符串长度、内容等n整数关系的测试:比较大小、相等判断等n上述三种关系通过逻辑运算符(与、或、非)的组合24常用的文件属性条件判断条件作用-f fnfn存

13、在且存在且fn为普通文件则返回真为普通文件则返回真-bfnfn存在且存在且fn为块设备则返回真为块设备则返回真-e fnfn存在则返回真存在则返回真-d fnfn存在且存在且fn为目录则返回真为目录则返回真-r fnfn存在且存在且fn可读则返回真可读则返回真-w fnfn存在且存在且fn可写则返回真可写则返回真-x fnfn存在且存在且fn可执行则返回真可执行则返回真-O fnfn存在且被当前用户拥有则返回真存在且被当前用户拥有则返回真-L fnfn存在且存在且fn为符号链接则返回真为符号链接则返回真25条件判断实例o分别用两种格式对当前目录下的目录和文件进行测试$ls l a_file.s

14、h-rw-r-r-1 tom users 0 2 14:33a_file.sh$test w a_file.sh$echo$?0$-w a_file.sh$echo$?026条件判断中的逻辑运算符o-a(逻辑与)n格式:“condition1 a condition2”,若两个条件都为真,则结果为真o-o(逻辑或)n格式:“condition1 o condition2”,若两个条件一个为真,则结果为真o!(逻辑非)n格式:“!condition”,当条件为真时,结果为假,反之27逻辑运算符实例o利用逻辑运算对目录中的文件进行多个条件的判断$ls ldrwxr-xr-x 2 tom None

15、0 Dec 8 15:41 a_dir-rw-r-r-1 tom users 0 Dec 8 15:41 a_file.sh-r-r-r-1 tom users 0 Dec 8 15:41 a_file_2.sh$-d a_dir a w a_dir$echo$?0$-x a_file.sh o w a_file_2.sh$echo$?128字符串属性条件作用String_1=string2如果相等则返回真,反之String_1!=string2如果不相等则返回真,反之-z string如果长度为0则返回真,反之-n string如果长度不为0则返回真,反之string同-n string29

16、字符串属性实例o判断两个字符串变量存储的字符串是否相同$root_home=“/root”$tom_home=“/home/tom”$root_home=$tom_home$echo$?130整数关系条件判断条件作用num_1-eq num_2如果num_1num_2则返回真,反之num_1-ne num_2如果num_1num_2则返回真,反之num_1-gt num_2如果num_1num_2则返回真,反之num_1-lt num_2如果num_1num_2则返回真,反之num_1-le num_2如果num_1num_2则返回真,反之num_1-ge num_2如果num_1num_2则

17、返回真,反之31整数关系条件判断实例o对两个整数变量进行比较$int_var_1=200$int_var_2=300$int_var_1-eq$int_var_2$echo$?1$int_var_1-ne$int_var_2$echo$?032多路分支语句if分支语句分支语句,格式如下:格式如下:if 条件条件1then命令命令/elif 条件条件2then命令命令/之内的部分可以省略else命令命令/fi33多路分支语句说明o如果表达式为真(0),则执行命令表中的命令;否则退出if语句,即执行elif后面的语句oif和fi是条件语句的语句括号,必须成对使用o命令表中的命令可以是一条,也可以是

18、若干条34 1#!/bin/bash 2 clear 3 echo input a file or directory name,please!4 read path_name 5 if -d$path_name 6 then 7 echo path_name is a directory 8 elif -f$path_name 9then 10 echo$path_name is a regular file 11 if -r$path_name 12then 13 echo$path_name is a readable file 14 fi 15 if -w$path_name 16 t

19、hen 17 echo$path_name is a writeable file 18 fi 19 if -x$path_name 20 then 21 echo$path_name is a executable file 22 fi 23 else 24 echo this script cannot get the file/directory($path_name)information!25 fi35程序运行$sh if.shinput a file or directory name,please!dirpath_name is a directoryinput a file o

20、r directory name,please!file.txtfile.txt is a regular filefile.txt is a readable filefile.txt is a writeable file$ls-l file.txt-rw-r-r-1 tom None 12 Dec 8 17:08 file.txt36if分支程序例子#!/bin/bashecho input a directory,please!read dir_nameif cd$dir_name /dev/null 2&1;then#0 表示键盘输入表示键盘输入 1表示屏幕输出表示屏幕输出 2表示错

21、误输出表示错误输出 echo enter directory:$dir_name succeedelse echo enter directory:$dir_name faildedfi37程序运行$mkdir test$sh if2.shinput a directory,please!testenter directory:test succeed$sh if2.shinput a directory,please!bookenter directory:book failded38case分支语句p格式:case条件条件in模式模式1)命令命令1;模式模式2)命令命令2;模式模式n)命令

22、命令n;esac39case例子o#!/bin/bashoclearoecho do you like Linux?oread like_itocase$like_it inoy|Y|yes|Yes|YES)echo Linux is a good friend of us.o ;on|N|no|No|NO)echo Try it,and you will like it.o ;o*)echo you answer is:$like_ito ;oesac40程序运行o$sh case.shodo you like Linux?oyoLinux is a good friend of us.o$

23、sh case.shodo you like Linux?onoTry it,and you will like it.o$sh case.shodo you like Linux?oI like it!oyou answer is:I like it!41循环语句ofor循环语句o格式:for 变量in 列表do命令(通常用到循环变量)doneo变量依次取列表中的第一个值到最后一个值,如果省略中括号中的部分,bash认为是“in$”42for循环例子#!/bin/bashclearfor os in Linux windows UNIXdo echo Operation System is:

24、$osdone$sh for.shOperation System is:LinuxOperation System is:windowsOperation System is:UNIX43while和until循环语句owhile和until循环适合在循环次数不确定的情况下使用o格式:owhile/until 条件odon命令odone44while循环和until循环示例while循环until循环1#!/bin/bash1#!/bin/bash2 clear2 clear3 loop=03 loop=04 while$loop ne 10 4 until$loop eq 10 5 do5

25、 do6 let loop=$loop+16 let loop=$loop+17 echo“current value of loop is:$loop”7 echo“current value of loop is:$loop”8 done8 done45函数o定义函数格式:ofunction 函数名函数名()o命令p“function”可以省,通常在程序执行语句之前定义函数p调用函数格式:p函数名函数名参数参数1参数参数2参数参数n46向函数传递参数示例#!/bin/bashFunction demo_fun()echo“your command is:$0$*”echo“Number o

26、f Parameters($#)is:$#”echo“Script file name($0)is:$0”echo“Parameters($*)is:$*”echo“Parameters($)is:$”count=1for param in$doecho“Parameters($count)is:$param”let count=$count+1donecleardemo_fun$47利用shift访问参数变量function demo_fun()while -n“$1”doecho“Parameters($count)is:$1”let count=$count+1shiftdone48Re

27、turn的使用o格式格式oreturn 0n正常结束oreturn 1或其他非0值n错误结束oreturnn以函数中最后一条命令的执行状态作为返回值 49return语句的使用1.#!/bin/bash2.function fun_return()3.4.dir_name=$15.rtn_value=06.if!cd$dir_name /dev/null 2&1;then7.rtn_value=18.fi9.return$rtn_value#5-9 equal to cd$dir_name /dev/null#2&1#return10.11.clear12.if fun_return$;the

28、n13.echo“function executes successfully!”14.else15.echo“function executes failes!”16.fi50shell程序实例(/.bashrc文件)1.#.bashrc2.#User specific aliases and functions3.#Source global definitions4.if -f/etc/bashrc;then5./etc/bashrc6.fi7.echo“Health is better than Wealth!”8.#t=time,h=hostname,w=current direct

29、ory9.export PS1=“tuh/w”51文件备份脚本示例1.#!/bin/bash2.#an example script of backup files3.LOG_START_TIME=date+%Y%m%d%H%M%S4.BACKUP_DIR=/backup5.BACKUP_LOG=$BACKUP_DIR/$LOG_START_TIME.log526.function write_log()7.8.log_time=data+%Y-%m-%d-%H-%M-%S9.backup_file_name=$210.err_msg=$log_time ERROR in backup fil

30、e/directory($backup_file_name)“11.suc_msg=$log_time SUCCESS in backup file/directory($backup_file_name)“12.if$1-eq 0 ;then13.echo$suc_msg14.echo$suc_msg$BACKUP_LOG15.else16.echo$err_msg17.echo$err_msg$BACKUP_LOG18.fi19.5320.function backup_file()21.22.cp-fr$1$BACKUP_DIR /dev/null 2&123.write_log$?$1

31、24.25.function create_log_file()26.27.if !-e$BACKUP_DIR ;then28.mkdir$BACKUP_DIR29.fi30.if -e$BACKUP_LOG;then31.rm-f$BACKUP_LOG32.fi33.touch$BACKUP_LOG34.5435.clear36.echo Backup Process Begins37.create_log_file38.for file in$39.do40.backup_file$file41.done 42.echo Backup Process Ends55程序运行$./mcp.sh a_dir file1.sh file2.sh2006-12-5-16-21-24 SUCCESS in backup file/directory(a_dir)2006-12-5-16-21-24 SUCCESS in backup file/directory(file1.sh)2006-12-5-16-21-24 SUCCESS in backup file/directory(file2.sh)Backup Process Ends56

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 技术资料 > 其他杂项

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知得利文库网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com