Shell脚本编程基础知识PPT课件.ppt

上传人:小飞机 文档编号:1966108 上传时间:2022-12-28 格式:PPT 页数:74 大小:216.50KB
返回 下载 相关 举报
Shell脚本编程基础知识PPT课件.ppt_第1页
第1页 / 共74页
Shell脚本编程基础知识PPT课件.ppt_第2页
第2页 / 共74页
Shell脚本编程基础知识PPT课件.ppt_第3页
第3页 / 共74页
Shell脚本编程基础知识PPT课件.ppt_第4页
第4页 / 共74页
Shell脚本编程基础知识PPT课件.ppt_第5页
第5页 / 共74页
点击查看更多>>
资源描述

《Shell脚本编程基础知识PPT课件.ppt》由会员分享,可在线阅读,更多相关《Shell脚本编程基础知识PPT课件.ppt(74页珍藏版)》请在三一办公上搜索。

1、Linux 操作系统,Shell 脚本编程,主要内容和学习要求,掌握创建 shell 脚本的基本步骤 学会使用条件测试 掌握 if 条件结构与 case 选择结构 掌握 for 循环、while 循环和 until 循环结构 学会 shift 命令的使用 学会 shell 脚本的调试,Shell 脚本,Shell 脚本,如果有一系列你经常使用的Linux命令,你可以把它们存储在一个文件里,shell可以读取这个文件并顺序执行其中的命令,这样的文件被称为脚本文件。shell 脚本按行解释。,Shell 脚本的编写,Shell 脚本是纯文本文件,可以使用任何文本编辑器编写 Shell 脚本通常是以

2、 .sh 作为后缀名,Shell 脚本的执行,chmod +x script_name./script_name,bash script_name,第一行:指定用哪个程序来编译和执行脚本。,Shell 脚本的格式,#!/bin/bash,可执行语句和 shell 控制结构,注释:以 “ # ” 开头,可独占一行,或跟在语句的后面。,Shell 脚本,#!/bin/sh,#!/bin/csh,一个 shell 脚本通常由一组 Linux 命令、shell 命令、控制结构和注释语句构成。,在脚本中多写注释语句是一个很好的编程习惯,#!/bin/bash# This is the first Bas

3、h shell program # ScriptName: greetings.shechoecho e Hello $LOGNAME, cecho its nice talking to you.echo Your present working directory is:pwd # Show the name of present directoryechoecho e The time is date +%T!. nByeecho,bash greetings.sh,chmod +x greetings.sh./greetings,Shell 脚本举例,echo命令,功能说明:显示文字。

4、 语 法:echo -ne字符串 或 echo -help-version 补充说明:echo会将输入的字符串送往标准输出。输出的字符串间以空白字符隔开, 并在最后加上换行号。-n 不进行换行-e 若字符串中出现以下字符,则特别加以处理,而不会将它当成一般文字输出 n 换行 b 空格.,参 数:,-n 不要在最后自动换行 -e 若字符串中出现以下字符,则特别加以处理,而不会将它当成一般文字输出:a 发出警告声; b 删除前一个字符; c 最后不加上换行符号; f 换行但光标仍旧停留在原来的位置; n 换行且光标移至行首; r 光标移至行首,但不换行; t 插入tab; v 与f相同; 插入字符

5、; nnn 插入nnn(八进制)所代表的ASCII字符; -help 显示帮助 -version 显示版本信息,#!/bin/bash# This script is to test the usage of read# Scriptname: ex4read.shecho = examples for testing read =echo -e What is your name? cread nameecho Hello $nameechoecho -n Where do you work? readecho I guess $REPLY keeps you busy!echoread -

6、p Enter your job title: #自动读给REPLYecho I thought you might be an $REPLY.echoecho = End of the script =,Shell 脚本举例,read命令,read variable #读取变量给variableread x y #可同时读取多个变量read #自动读给REPLYread p “Please input: ” #自动读给REPLY,状态变量 $? 中保存命令退出状态的值,grep $USER /etc/passwdecho $?grep hello /etc/passwd; echo $?,条

7、件测试,条件测试可以根据某个特定条件是否满足,来选择执行相应的任务。,Bash 中允许测试两种类型的条件: 命令成功或失败,表达式为真或假,任何一种测试中,都要有退出状态(返回值),退出状态为 0 表示命令成功或表达式为真,非0 则表示命令失败或表达式为假。,内置测试命令 test,通常用 test 命令来测试表达式的值,x=5; y=10test $x -gt $yecho $?,test 命令可以用 方括号 来代替,x=5; y=10 $x -gt $y echo $?,表达式测试包括字符串测试、整数测试和文件测试。,测试表达式的值,方括号前后要留空格!,name=Tom $name =

8、Tt? echo $?,2.x 版本以上的 Bash 中可以用双方括号来测试表达式的值,此时可以使用通配符进行模式匹配。,测试表达式的值, $name = Tt? echo $?,字符串测试,name=Tom; -z $name ; echo $?,操作符两边必须留空格!,字符串测试,name2=Andy; $name = $name2 ; echo $?,整数测试,即比较大小,x=1; $x -eq 1 ; echo $?,x=a; $x -eq 1 ; echo $?,整数测试,操作符两边必须留空格!,X,整数测试也可以使用 let 命令或双圆括号,x=1; let $x = 1; ech

9、o $?,x=1; ($x+1= 2 ); echo $?,只能用于整数测试!,整数测试,相应的操作符为:,= 、!= 、 、= 、 、=,例:,两种测试方法的区别,使用的操作符不同 let 和 双圆括号中可以使用算术表达式,而中括号不能 let 和 双圆括号中,操作符两边可以不留空格,逻辑测试,x=1; name=Tom; $x -eq 1 a n $name ; echo $?,逻辑测试,注:不能随便添加括号, ( $x -eq 1 ) a ( n $name ) ; echo $?,X,x=1; name=Tom; $x -eq 1 echo $?,可以使用模式的逻辑测试,逻辑测试,文件

10、测试:文件是否存在,文件属性,访问权限等。,常见的文件测试操作符,更多文件测试符参见 test 的在线帮助,man test,文件测试,检查空值, $name = , ! $name , X$name != X ,检查空值,语法结构,if expr1 # 如果expr1 为真(返回值为0)then # 那么 commands1 # 执行语句块 commands1elif expr2 # 若expr1 不真,而expr2 为真then # 那么 commands2 # 执行语句块 commands2 . . # 可以有多个 elif 语句 else # else 最多只能有一个 commands

11、4 # 执行语句块 commands4fi # if 语句必须以单词 fi 终止,if 条件语句,commands 为可执行语句块,如果为空,需使用 shell 提供的空命令 “ : ”,即冒号。该命令不做任何事情,只返回一个退出状态 0,if 语句可以嵌套使用,ex4if.sh,chkperm.sh,chkperm2.sh, name_grep,tellme,tellme2,idcheck.sh,几点说明,elif 可以有任意多个(0 个或多个),else 最多只能有一个(0 个或 1 个),if 语句必须以 fi 表示结束,expr 通常为条件测试表达式;也可以是多个命令,以最后一个命令的

12、退出状态为条件值。,ex4if.sh,#!/bin/bash# scriptname: ex4if.sh#echo -n Please input x,y: read x yecho x=$x, y=$yif ( x y ); then echo x is larger than yelif ( x = y); then echo x is equal to yelse echo x is less than yfi,chkperm.sh,#!/bin/bash# Using the old style test command: # filename: perm_check.sh#file=

13、testingif -d $file then echo $file is a directory elif -f $file then if -r $file -a -w $file -a -x $file then # nested if command echo You have read,write,and execute permission on $file. fielse echo $file is neither a file nor a directory. fi,chkperm2.sh,#!/bin/bash# Using the new style test comman

14、d: # filename: perm_check2.sh#file=./testingif -d $file then echo $file is a directory elif -f $file then if -r $file & -w $file & -x $file then # nested if command echo You have read,write,and execute permission on $file. fielse echo $file is neither a file nor a directory. fi,name_grep,#!/bin/bash

15、# filename: name_grep#name=Tomif grep $name /etc/passwd & /dev/null then :else echo $name not found in /etc/passwd exit 2fi,tellme,#!/bin/bashecho -n How old are you? read ageif $age -lt 0 -o $age -gt 120 thenecho Welcome to our planet! exit 1 fiif $age -ge 0 -a $age -le 12 thenecho Children is the

16、flowers of the countryelif $age -gt 12 -a $age -le 19 thenecho Rebel without a causeelif $age -gt 19 -a $age -le 29 then echo You got the world by the tail!elif $age -ge 30 -a $age -le 39 thenecho Thirty something.elseecho Sorry I askedfi,tellme2,#!/bin/bashecho -n How old are you? read ageif ( age

17、120 ) thenecho Welcome to our planet! exit 1 fiif (age = 0 & age = 13 & age = 19 & age = 30 & age = 39 )thenecho Thirty something.elseecho Sorry I askedfi,idcheck.sh,#!/bin/bash# Scriptname: idcheck.sh# purpose: check user id to see if user is root.# Only root has a uid of 0.# Format for id output:

18、uid=501(tt) gid=501(tt) groups=501(tt)# roots uid=0 : uid=0(root) gid=0(root) groups=0(root)#id=id | awk -F=( print $2 # get user idecho your user id is: $idif ( id = 0 ) # $id -eq 0 then echo you are superuser.elseecho you are not superuser.fi,语法结构,case expr in # expr 为表达式,关键词 in 不要忘! pattern1) # 若

19、 expr 与 pattern1 匹配,注意括号 commands1 # 执行语句块 commands1 ; # 跳出 case 结构 pattern2) # 若 expr 与 pattern2 匹配 commands2 # 执行语句块 commands2 ; # 跳出 case 结构 . . # 可以有任意多个模式匹配 *) # 若 expr 与上面的模式都不匹配 commands # 执行语句块 commands ; # 跳出 case 结构esac # case 语句必须以 esac 终止,case 选择语句,case 语句举例:yes_no.sh,几点说明,每个命令块的最后必须有一个双

20、分号,可以独占一行,或放在最后一个命令的后面。,所给的匹配模式 pattern 中可以含有通配符和“ | ”。,如果 expr 没有找到匹配的模式,则执行缺省值 “ *) ” 后面的命令块 ( 类似于 if 中的 else ); “ *) ” 可以不出现。,表达式 expr 按顺序匹配每个模式,一旦有一个模式匹配成功,则执行该模式后面的所有命令,然后退出 case。,yes_no.sh,#!/bin/bash# test case # scriptname: yes_no.sh#echo -n Do you wish to proceed y/n: read anscase $ans in y

21、|Y|yes|Yes) echo yes is selected ; n|N|no|No) echo no is selected ; *) echo basename $0: Unknown response exit 1 ;esac,语法结构,for variable in list # 每一次循环,依次把列表 list 中的一个值赋给循环变量do # 循环开始的标志 commands # 循环变量每取一次值,循环体就执行一遍done # 循环结束的标志,几点说明,列表 list 可以是命令替换、变量名替换、字符串和文件名列表 ( 可包含通配符 ) for 循环执行的次数取决于列表 lis

22、t 中单词的个数 for 循环体中一般要出现循环变量,但也可以不出现,for 循环语句,执行第一轮循环时,将 list 中的第一个词赋给循环变量,并把该词从 list 中删除,然后进入循环体,执行 do 和 done 之间的命令。下一次进入循环体时,则将第二个词赋给循环变量,并把该词从 list 中删除,再往后的循环也以此类推。当 list 中的词全部被移走后,循环就结束了。,循环执行过程,forloop.sh,mybackup.sh,位置参量的使用: $* 与 $,greet.sh,可以省略 in list ,此时使用位置参量,permx.sh idcheck.sh greet.sh yes

23、_no.sh permx.sh *.sh,for 循环执行过程,forloop.sh,#!/bin/bash# Scriptname: forloop.shfor name in Tom Dick Harry Joedo echo Hi $namedoneecho out of loop,forloop2.sh,#!/bin/bash# Scriptname: forloop2.shfor name in cat namelistdo echo Hi $namedoneecho out of loop,mybackup.sh,#!/bin/bash# Scriptname: mybackup.

24、sh# Purpose: Create backup files and store# them in a backup directory.# backup_dir=backupmkdir $backup_dirfor file in *.sh do if -f $file then cp $file $backup_dir/$file.bak echo $file is backed up in $backup_dir fidone,greet.sh,#!/bin/bash# Scriptname: greet.sh# usage: greet.sh Tom John Anndyecho

25、= using $* =for name in $* # same as for name in $do echo Hi $name doneecho = using $ =for name in $ # same as for name in $*do echo Hi $name doneecho = using $* =for name in $*do echo Hi $name doneecho = using $ =for name in $do echo Hi $name done,permx.sh,#!/bin/bash# Scriptname: permx.sh#for file

26、 # Empty wordlistdo if -f $file & ! -x $file then chmod +x $file echo = $file now has execute permission fidone,语法结构,while expr # 执行 exprdo # 若 expr 的退出状态为0,进入循环,否则退出while commands # 循环体done # 循环结束标志,返回循环顶部,执行过程,先执行 expr,如果其退出状态为 0,就执行循环体。执行到关键字 done 后,回到循环的顶部,while 命令再次检查 expr 的退出状态。以此类推,循环将一直继续下去,

27、直到 expr 的退出状态非 0 为止。,while 循环语句,语法结构,until expr # 执行 exprdo # 若expr的退出状态非0,进入循环,否则退出until commands # 循环体done # 循环结束标志,返回循环顶部,执行过程,与 while 循环类似,只是当 expr 退出状态非 0 时才执行循环体,直到 expr 为 0 时退出循环。,until 循环语句,用于强行退出当前循环。 如果是嵌套循环,则 break 命令后面可以跟一数字 n,表示退出第 n 重循环(最里面的为第一重循环)。,用于忽略本次循环的剩余部分,回到循环的顶部,继续下一次循环。 如果是嵌套

28、循环,continue 命令后面也可跟一数字 n,表示回到第 n 重循环的顶部。,break n,continue n,例:months.sh,break 和 continue,months.sh,#!/bin/bash# Scriptname: months.shfor month in Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Decdo for week in 1 2 3 4 do echo -n Processing the month of $month. OK? read ans if $ans = n -o -z $ans then

29、continue 2 else,echo -n Process week $week of $month? read ans if $ans = n -o -z $ans then continue else echo Now processing week $week of $month. sleep 1 # Commands go here echo Done processing. fi fi donedone,sleep n,exit 和 sleep,exit n,exit 命令用于退出脚本或当前进程。n 是一个从 0 到 255 的整数,0 表示成功退出,非零表示遇到某种失败而非正常

30、退出。该整数被保存在状态变量 $? 中。,exit 命令,sleep 命令,暂停 n 秒钟,语法结构,说明,select 循环主要用于创建菜单,按数字顺序排列的菜单项将显示在标准错误上,并显示 PS3 提示符,等待用户输入 用户输入菜单列表中的某个数字,执行相应的命令 用户输入被保存在内置变量 REPLY 中。,select variable in list do # 循环开始的标志 commands # 循环变量每取一次值,循环体就执行一遍done # 循环结束的标志,例:runit.sh,select 循环与菜单,runit.sh,#!/bin/bash# Scriptname: runi

31、t.shPS3=Select a program to execute: select program in ls -F pwd datedo $programdone,例:goodboy.sh,select 经常和 case 联合使用,select 是个无限循环,因此要记住用 break 命令退出循环,或用 exit 命令终止脚本。也可以按 ctrl+c 退出循环。,与 for 循环类似,可以省略 in list ,此时使用位置参量,select 与 case,goodboy.sh,#!/bin/bash# Scriptname: goodboys.shPS3=Please choose o

32、ne of the three boys : select choice in tom dan guy#select choice do case $choice in tom) echo Tom is a cool dude! break; # break out of the select loop dan | guy ) echo Dan and Guy are both wonderful. break; *) echo $REPLY is not one of your choices echo Try again. ; esacdone,shift n,用于将参量列表 list 左

33、移指定次数,缺省为左移一次。 参量列表 list 一旦被移动,最左端的那个参数就从列表中删除。while 循环遍历位置参量列表时,常用到 shift。,./doit.sh a b c d e f g h,./shft.sh a b c d e f g h,循环控制 shift 命令,例:,doit.sh,#!/bin/bash# Name: doit.sh# Purpose: shift through command line arguments# Usage: doit.sh argswhile ( $# 0 ) # or $# -gt 0 do echo $* shiftdone,shf

34、t.sh,#!/bin/bash# Using shift to step through all the positional parameters.until -z $1 # Until all parameters used up.do echo $1 shiftdoneecho # Extra line feed.exit 0,生成随机数的特殊变量,echo $RANDOM 范围是: 0, 32767,expr:通用的表达式计算命令,表达式中参数与操作符必须以空格分开,表达式中的运算可以是算术运算,比较运算,字符串运算和逻辑运算。,expr 5 % 3,expr 5 * 3 # 乘法符

35、号必须被转义,随机数和 expr 命令,字符串操作,注:pattern,old 中可以使用通配符。,例:ex4str,m 的取值从 0 到 $#var-1,字符串操作,ex4str,#!/bin/bashdirname=/usr/bin/local/bin; echo dirname=$dirnameecho -n $#dirname=; sleep 4;echo $#dirnameechoecho -n $dirname:4=; sleep 4;echo $dirname:4echoecho -n $dirname:8:6=; sleep 4; echo $dirname:8:6echoec

36、ho -n $dirname#*bin=; sleep 4; echo $dirname#*binechoecho -n $dirname#*bin=; sleep 4;echo $dirname#*binechoecho -n $dirname%bin=; sleep 4;echo $dirname%binechoecho -n $dirname%bin=; sleep 4;echo $dirname%binechoecho -n $dirname%bin*=; sleep 4;echo $dirname%bin*echoecho -n $dirname%bin*=; echo $dirna

37、me%bin*echoecho -n $dirname/bin/sbin=; echo $dirname/bin/sbinechoecho -n $dirname/bin/lib=; echo $dirname/bin/libechoecho -n $dirname/bin*/lib=; echo $dirname/bin*/lib,sh x 脚本名,该选项可以使用户跟踪脚本的执行,此时 shell 对脚本中每条命令的处理过程为:先执行替换,然后显示,再执行它。shell 显示脚本中的行时,会在行首添加一个加号 “ + ”。,sh v 脚本名,在执行脚本之前,按输入的原样打印脚本中的各行,打印

38、一行执行一行。,sh n 脚本名,对脚本进行语法检查,但不执行脚本。如果存在语法错误,shell 会报错,如果没有错误,则不显示任何内容。,脚本调试,编程小结:变量,局部变量、环境变量(export、declare -x),只读变量、整型变量,例:declare -i x; x=hello; echo $x,0,位置参量($0,$1,.,$*,$,$#,$,$?),变量的间接引用(eval, $!str),例:name=hello; x=name; echo $!x,hello,命令替换(cmd、$(cmd)),整数运算 declare 定义的整型变量可以直接进行运算, 否则需用 let 命令

39、或 $.、$(.) 进行整数运算。,编程小结:输入输出,输入:read,read var1 var2 .,read,read p 提示,输出:printf,printf %-12.5f t %d n 123.45 8,format以%开头,flag,field width,precision,格式符,-:左对齐+:输出符号0:空白处添0空格:前面加一空格,字段宽度,小数点后输出位数,cdefgsox,bnrtv”%, REPLY, REPLY,输出参数用空格隔开,字符串测试,编程小结:条件测试,操作符两边必须留空格!,如果使用双方括号,可以使用 通配符 进行模式匹配。,例:name=Tom;

40、$name Tom ; echo $?,编程小结:条件测试,整数测试,注意这两种方法的区别!,编程小结:条件测试,逻辑测试,如果使用双方括号,可以使用 通配符 进行模式匹配。,编程小结:条件测试,文件测试,编程小结:控制结构,if 条件语句 case 选择语句 for 循环语句 while 循环语句 until 循环语句 break、continue、sleep 命令 select 循环与菜单 shift 命令,$.,$(.),$.,$(.) .,.,(.),各种括号的作用,function_name () commands,函数,一个函数就是一个子程序,用于完成特定的任务,当有重复代码,或者

41、一个任务只需要很少的修改就被重复几次执行时, 这时你应考虑使用函数。,function function_name commands ,函数的一般格式,和其它编程语言一样, Bash 也可以定义函数。,函数举例,#!/bin/bashfun1 () echo This is a functionecho Now exiting fun1. fun2 () echo This is fun2. echo Now exiting fun2.,只需输入函数名即可调用该函数。,函数的调用,函数必须在调用之前定义,#!/bin/bashfun2 () echo This is fun2. echo No

42、w exiting fun2.fun2 # 调用函数 fun2,例:ex4fun2.sh, ex4fun3.sh,ex4fun2.sh,#!/bin/bashJUST_A_SECOND=1fun () # A somewhat more complex function i=0 REPEATS=5 echo echo And now the fun really begins. echo sleep $JUST_A_SECOND # Hey, wait a second! while $i -lt $REPEATS do echo -FUNCTIONS- echo echo let i+=1

43、done# Now, call the functions.funexit 0,ex4fun3.sh,# f1 # Will give an error message, since function f1 not yet defined.# declare -f f1 # This doesnt help either.# f1 # Still an error message.# However.f1 () echo Calling function f2 from within function f1.f2f2 () echo Function f2.# f1 # Function f2

44、 is not actually called until this point# although it is referenced before its definition.# This is permissible.,向函数传递参数,函数的调用,例:ex4fun4.sh,函数与命令行参数,例:ex4fun5.sh,return 与 exit,例:ex4fun6.sh,向函数传递参数 例:ex4fun4.sh,#!/bin/bash# Functions and parametersDEFAULT=default # Default param value.func2 () if -z

45、$1 # Is parameter #1 zero length? then echo -Parameter #1 is zero length - else echo -Param #1 is $1 -fivariable=$1:-$DEFAULT echo variable = $variable if -n $2 then echo - Parameter #2 is $2 -fireturn 0,echoecho Nothing passedfunc2 # Called with no paramsechoecho One parameter passed.func2 first #

46、Called with one paramechoecho Two parameters passed.func2 first second # Called with two paramsechoecho second passed.func2 second # The first parameter is of zero?lengthecho exit 0# End of script,函数与命令行参数 例:ex4fun5.sh,#!/bin/bash# function and command line arguments# Call this script with a command

47、 line argument,# something like $0 arg1.func ()echo $1echo First call to function: no arg passed.echo See if command-line arg is seen.Func # No! Command-line arg not seen.echo =echoecho Second call to function: command-line arg passed explicitly.func $1# Now its seen!exit 0,return 与 exit 例:ex4fun6.s

48、h,#!/bin/bash# purpose: Maximum of two integers.max2 () # Returns larger of two numbers.if -z $2 then echo Need to pass two parameters to the function. exit 1 fi if $1 = $2 # $1 -eq $2 then echo The two numbers are equal. exit 0 else if $1 -gt $2 then return $1 else return $2 fi fi,read num1 num2echo num1=$num1, num2=$num2max2 $num1 $num2return_val=$?echo The larger of the two numbers is: $return_val.exit 0,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号