Shell脚本编程基础知识PPT课件.ppt
《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
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Shell 脚本 编程 基础知识 PPT 课件
![提示](https://www.31ppt.com/images/bang_tan.gif)
链接地址:https://www.31ppt.com/p-1966108.html