版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Ch2 Shell Programming,Jianjian SONG Software Institute, Nanjing University Sept. 2004,Content,Shell Overview What is Shell? Why Shell? 创建和执行Shell程序 Shell程序设计的语法 变量、条件测试、条件语句、重复语句、命令表和语句块、函数 实例,1. Shell Overview,Shell 用户和操作系统之间的接口 作为核外程序而存在,Shell: 用户和操作系统之间的接口,csh 其它程序 bash X窗口系统,Shell: 作为核外程序而存在,内核,
2、各种不同的Shell,Shell 的双重角色,命令解释程序 Linux的开机启动过程;进程树 Shell的工作步骤 打印提示符;得到命令行;解析命令;查找文件;准备参数;执行命令 独立的程序设计语言解释器 KISS (Keep It Small and Stupid) Reusable tools Redirection and pipe,UNIXs Philosophy (Examples),Redirection Use “echo” to create a file? Pipe Get the count of files in a directory? Show subdirector
3、ies and only subdirectories? ar t /usr/lib/libc.a | grep printf | pr -4 -t (?),2. 创建和执行Shell程序,编写脚本文件 执行脚本文件 运行环境,编写脚本文件,脚本文件 注释 退出码(exit code) Example,#!/bin/bash # Here is comments for file in *; do if grep POSIX $file; then more $file fi done exit 0,执行脚本文件,方法1: $ sh script_file 方法2: chmod +x scri
4、pt_file (chown, chgrp optionally) ./script_file 方法3: source script_file, or . script_file,用户环境,.bash_profile, .bash_logout, .bashrc files .bash_profile: 用户登录时被读取,其中包含的命令被bash执行 .bashrc: 启动一个新的shell时读取并执行 .bash_logout: 登录退出时读取执行 Alias alias/unalias command 环境变量 export command export, env (同一行上多个命令的分隔
5、符),if语句(2),例1(.bash_profile文件中) if -f /.bashrc ; then . /.bashrc fi 例2 #!/bin/sh echo “Is this morning? Please answer yes or no.” read answer if “$answer” = “yes” ; then echo “Good morning” elif “$answer” = “no” ; then echo “Good afternoon” else echo “Sorry, $answer not recognized. Enter yes or no”
6、exit 1 fi exit 0,case语句(1),形式 case str in str1 | str2) statements; str3 | str4) statements; *) statements; esac,case语句(2),Example #!/bin/sh echo “Is this morning? Please answer yes or no.” read answer case “$answer” in yes | y | Yes | YES) echo “Good morning!” ; no | n | No | NO) echo “Good afternoo
7、n!” ; *) echo “Sorry, answer not recognized.” ; esac exit 0,3.4 重复语句,for语句 while语句 until语句 select语句,for语句(1),形式 for var in list do statements done 适用于对一系列字符串循环处理,for语句(2),Example #!/bin/sh for file in $(ls f*.sh); do lpr $file done exit 0,while语句(1),形式 while condition do statements done,while语句(2),E
8、xample quit=n while “$quit” != “y” ; do read menu_choice case “$menu_choice” in a) do_something; b) do_anotherthing; q|Q) quit=y; *) echo “Sorry, choice not recognized.”; esac done,until语句,形式 until condition do statements done Not recommended (while statement is preferred),select语句(1),形式 select item
9、 in itemlist do statements done 作用 生成菜单列表,select语句(2),例(一个简单的菜单选择程序) #!/bin/sh clear select item in Continue Finish do case “$item” in Continue) ; Finish) break ; *) echo “Wrong choice! Please select again!” ; esac done Question: 用while语句模拟?,3.5 命令表和语句块,命令表 命令组合 语句块,命令表,命令组合 分号串联 command1 ; command2
10、 ; 条件组合 AND命令表 格式:statement1 statement2 ; ; ,3.6 函数,形式 func() statements 局部变量 local关键字 函数的调用 func para1 para2 返回值 return,函数的例子(1): 定义,yesno() msg=“$1” def=“$2” while true; do echo” ” $echol “$msg” read answer if “$answer” ; then else return $def fi done ,case “$answer” in y|Y|yes|YES) return 0 ; n|N
11、|no|NO) return 1 ; *) echo “ ” echo “ERROR: Invalid response, expected ”yes” or ”no”.” continue ; esac,函数的例子(2): 使用,调用函数yesno if yesno “Continue installation? n” 1 ; then : else exit 1 fi,3.7 其它,杂项命令 break, continue, exit, return, export, set, unset, trap, “:”, “.”, 捕获命令输出 算术扩展 参数扩展 即时文档,杂项命令,break:
12、 从for/while/until循环退出 continue: 跳到下一个循环继续执行 exit n: 以退出码”n”退出脚本运行 return: 函数返回 export: 将变量导出到shell,使之成为shell的环境变量 set: 为shell设置参数变量 unset: 从环境中删除变量或函数 trap: 指定在收到操作系统信号后执行的动作 “:”(冒号命令): 空命令 “.”(句点命令)或source: 在当前shell中执行命令,捕获命令输出,语法 $(command) command 举例 #!/bin/sh echo “The current directory is $PWD”
13、 echo “The current directory is $(pwd)” exit 0,算术扩展,expr命令 $()扩展 #!/bin/sh x=0 while “$x” ne 10 ; do echo $x x=$($x+1) done exit 0,参数扩展,问题: 批处理 1_tmp, 2_tmp, 方法 #!/bin/sh i=0 while “$i” ne 10 ; do touch “$i_tmp” i=$($i+1) done exit 0 参数替换的复杂形式,即时文档,在shell脚本中向一条命令传送输入数据 Example #!/bin/bash cat !CATIN
14、PUT! Hello, this is a here document. !CATINPUT!,4. 实例分析,Install脚本 CD唱盘管理程序,设计,用文件进行数据存取 Text processing: grep command Compared with database,源代码分析(1),主程序 quit=n while “$quit” != “y” ; do set_menu_choice case “$menu_choice” in a) add_records; r) remove_records; f) find_cd y; u) update_cd; q|Q) quit=y; *) echo “Sorry, choice not recognized.”; esac done,源代码分析(2),全局变量 menu_choice, title_file, tracks_file, 功能模块 add_records: 输入CD唱盘标题信息 add_record_tracks: 输入CD唱盘曲目信息 find_cd
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 电子商务整体调试合同
- 2026-2027学年江苏省苏州市昆山市数学五下期末学业质量监测模拟试题含答案含解析
- 软件开发正式调试合同
- 广元市剑阁县2027届四上数学期末考试试题含解析
- 2026年英语必修三测试题及答案
- 2026年平安普惠 测试题及答案
- 2026重庆新智文旅有限公司招聘1人模拟试卷及参考答案详解(预热题)
- 2026四川南充市顺庆区事业单位引进高层次人才考核招聘10人备考题库含答案详解【B卷】
- 2026中国葛洲坝集团第一工程有限公司招聘18人笔试历年难易错考点试卷带答案解析
- 2025湖南湘电集团招聘100人笔试历年常考点试题专练附带答案详解
- 大连职业技术学院《小学语文课程标准与教材研究》2024-2025学年第一学期期末试卷
- 政法培训心理健康知识课件
- 农民工讨薪维权课件
- 面粉厂安全培训内容课件
- 煤矿井下喷浆安全培训课件
- 人教版物理九年级第14章第2节《热机的效率》听评课记录
- 神经外科护理小讲课
- 海外属地化员工管理制度
- 地震救援安全培训课件
- TCEC-抽水蓄能电站润滑油在线监测技术导则编制说明
- 敬业合同协议书范本下载
评论
0/150
提交评论