创建和执行Shell程序.ppt

  1. 1、本文档共53页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
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: 作为核外程序而存在 各种不同的Shell Shell 的双重角色 命令解释程序 Linux的开机启动过程;进程树 Shell的工作步骤 打印提示符;得到命令行;解析命令;查找文件;准备参数;执行命令 独立的程序设计语言解释器 KISS (Keep It Small and Stupid) Reusable tools Redirection and pipe UNIX’s Philosophy (Examples) Redirection Use “echo” to create a file? Pipe Get the count of files in a directory? Show subdirectories and only subdirectories? ar t /usr/lib/libc.a | grep printf | pr -4 -t (?) 2. 创建和执行Shell程序 编写脚本文件 执行脚本文件 运行环境 编写脚本文件 脚本文件 注释 退出码(exit code) Example 执行脚本文件 方法1: $ sh script_file 方法2: chmod +x script_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 set command 3. Shell程序设计的语法 变量 条件测试 条件语句 重复语句 命令表和语句块 函数 其它 3.1 变量 用户变量 环境变量 参数变量和内部变量 用户变量 变量的赋值和使用 var=value echo $var read命令 REPLY variable 引号的用法 双引号,单引号 转义符“\” 环境变量 参数变量和内部变量 3.2 条件测试 退出码 test命令 test expression 或 [ expression ] test命令支持的条件测试 字符串比较 算术比较 与文件有关的条件测试 逻辑操作 字符串比较 算术比较 与文件有关的条件测试 逻辑操作 3.3 条件语句 if语句 case语句 if语句(1) 形式 if [ expression ] then statements elif [ expression ] then statements elif … else statements fi 紧凑形式 ; (同一行上多个命令的分隔符) 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” 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

文档评论(0)

189****7685 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档