A Simple Introduction To Shell

      A shell is software that provides an interface for users of an operating system which provides access to the services of a kernel. However, the term is also applied very loosely to applications and may include any software that is "built around" a particular component, such as web browsers and email clients that are "shells" for HTML rendering engines. The name shell originates from shells being an outer layer of interface between the user and the internals of the operating system (the kernel).

      Operating system shells generally fall into one of two categories: command-line and graphical. Command-line shells provide a command-line interface (CLI) to the operating system, while graphical shells provide a graphical user interface (GUI). In either category the primary purpose of the shell is to invoke or "launch" another program; however, shells frequently have additional capabilities such as viewing the contents of directories.

      The relative merits of CLI- and GUI-based shells are often debated. CLI proponents claim that certain operations can be performed much faster under CLI shells than under GUI shells (such as moving files, for example). However, GUI proponents advocate the comparative usability and simplicity of GUI shells. The best choice is often determined by the way in which a computer will be used. On a server mainly used for data transfers and processing with expert administration , a CLI is likely to be the best choice. However, a GUI would be more appropriate for a computer to be used for image or video editing and the development of the above data.

      In expert systems, a shell is a piece of software that is an "empty" expert system without the knowledge base for any particular application.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
高级bash编程 高级Bash脚本编程指南(一) 目录 ++++ 第一部分. 热身 1. 为什么使用shell编程 2. 带着一个Sha-Bang出发(Sha-Bang指的是#!) 2.1. 调用一个脚本 2.2. 初步的练习 第二部分. 基本 3. 特殊字符 4. 变量和参数的介绍 4.1. 变量替换 4.2. 变量赋值 4.3. Bash变量是不分类型的 4.4. 特殊的变量类型 5. 引用(翻译的可能有问题,特指引号) 5.1. 引用变量 5.2. 转义(\) 6. 退出和退出状态 7. Tests 7.1. Test结构 7.2. 文件测试操作 7.3. 其他比较操作 7.4. 嵌套的if/then条件test 7.5. 检查你的test知识 8. 操作符和相关的主题 8.1. 操作符 8.2. 数字常量 第三部分. 超越基本 9. 变量重游 9.1. 内部变量 9.2. 操作字符串 9.3. 参数替换 9.4. 指定类型的变量:declare或者typeset 9.5. 变量的间接引用 9.6. $RANDOM: 产生随机整数 9.7. 双圆括号结构 10. 循环和分支 10.1. 循环 10.2. 嵌套循环 10.3. 循环控制 10.4. 测试与分支(case和select结构) 11. 内部命令与内建 11.1. 作业控制命令 12. 外部过滤器,程序和命令 12.1. 基本命令 12.2. 复杂命令 12.3. 时间/日期 命令 12.4. 文本处理命令 12.5. 文件与归档命令 12.6. 通讯命令 12.7. 终端控制命令 12.8. 数学计算命令 12.9. 混杂命令 13. 系统与管理命令 13.1. 分析一个系统脚本 14. 命令替换 15. 算术扩展 16. I/O 重定向 16.1. 使用exec 16.2. 代码块的重定向 16.3. 应用 17. Here Documents 17.1. Here Strings 18. 休息时间 Part 4. 高级 19. 正则表达式 19.1. 一个简要的正则表达式介绍 19.2. 通配 20. 子shell(Subshells) 21. 受限shell(Restricted Shells) 22. 进程替换 23. 函数 23.1. 复杂函数和函数复杂性 23.2. 局部变量 23.3. 不使用局部变量的递归 24. 别名(Aliases) 25. 列表结构 26. 数组 27. /dev 和 /proc 27.1. /dev 27.2. /proc 28. 关于Zeros和Nulls 29. 调试 30. 选项 31. Gotchas 32. 脚本编程风格 32.1. 非官方的Shell脚本风格 33. 杂项 33.1. 交互式和非交互式的shells和脚本 33.2. Shell 包装 33.3. 测试和比较: 另一种方法 33.4. 递归 33.5. 彩色脚本 33.6. 优化 33.7. 各种小技巧 33.8. 安全话题 33.8.1. 被感染的脚本 33.8.2. 隐藏Shell脚本源码 33.9. 移植话题 33.10. 在Windows下进行Shell编程 34. Bash, 版本 2 和 3 34.1. Bash, 版本2 34.2. Bash, 版本3 35. 后记 35.1. 作者后记 35.2. 关于作者 35.3. 哪里可以取得帮助? 35.4. 制作这本书的工具 35.4.1. 硬件 35.4.2. 软件和排版软件 35.5. Credits Bibliography A. Contributed Scripts B. Reference Cards C. A Sed and Awk Micro-Primer C.1. Sed C.2. Awk D. Exit Codes With Special Meanings E. A Detailed Introduction to I/O and I/O Redirection F. Standard Command-Line Options G. Important Files H. Important System Directories I. Localization J. History Commands K. A Sample .bashrc File L. Converting DOS Batch Files to Shell Scripts M. Exercises M.1. Analyzing Scripts M.2. Writing Scripts N. Revision History O. Mirror Sites P. To Do List Q. Copyright 表格清单: 11-1. 作业标识符 30-1. Bash 选项 33-1. 转义序列中数值和彩色的对应 B-1. Special Shell Variables B-2. TEST Operators: Binary Comparison B-3. TEST Operators: Files B-4. Parameter Substitution and Expansion B-5. String Operations B-6. Miscellaneous Constructs C-1. Basic sed operators C-2. Examples of sed operators D-1. "Reserved" Exit Codes L-1. Batch file keywords / variables / operators, and their shell equivalents L-2. DOS commands and their UNIX equivalents N-1. Revision History 例子清单: 2-1. 清除:清除/var/log下的log文件 2-2. 清除:一个改良的清除脚本 2-3. cleanup:一个增强的和广义的删除logfile的脚本 3-1. 代码块和I/O重定向 3-2. 将一个代码块的结果保存到文件 3-3. 在后台运行一个循环 3-4. 备份最后一天所有修改的文件. 4-1. 变量赋值和替换 4-2. 一般的变量赋值 4-3. 变量赋值,一般的和比较特殊的 4-4. 整型还是string? 4-5. 位置参数 4-6. wh,whois节点名字查询 4-7. 使用shift 5-1. echo一些诡异的变量 5-2. 转义符 6-1. exit/exit状态 6-2. 否定一个条件使用! 7-1. 什么情况下为真? 7-2. 几个等效命令test,/usr/bin/test,[],和/usr/bin/[ 7-3. 算数测试使用(( )) 7-4. test死的链接文件 7-5. 数字和字符串比较 7-6. 测试字符串是否为null 7-7. zmore 8-1. 最大公约数 8-2. 使用算术操作符 8-3. 使用&&和||进行混合状态的test 8-4. 数字常量的处理 9-1. $IFS和空白 9-2. 时间输入 9-3. 再来一个时间输入 9-4. Timed read 9-5. 我是root? 9-6. arglist:通过$*和$@列出所有的参数 9-7. 不一致的$*和$@行为 9-8. 当$IFS为空时的$*和$@ 9-9. 下划线变量 9-10. 在一个文本文件的段间插入空行 9-11. 利用修改文件名,来转换图片格式 9-12. 模仿getopt命令 9-13. 提取字符串的一种可选的方法 9-14. 使用参数替换和error messages 9-15. 参数替换和"usage"messages 9-16. 变量长度 9-17. 参数替换中的模式匹配 9-18. 重命名文件扩展名 9-19. 使用模式匹配来分析比较特殊的字符串 9-20. 对字符串的前缀或后缀使用匹配模式 9-21. 使用declare来指定变量的类型 9-22. 间接引用 9-23. 传递一个间接引用给awk 9-24. 产生随机数 9-25. 从一副扑克牌中取出一张随机的牌 9-26. 两个指定值之间的随机数 9-27. 使用随机数来摇一个骰子 9-28. 重新分配随机数种子 9-29. 使用awk产生伪随机数 9-30. C风格的变量处理 10-1. 循环的一个简单例子 10-2. 每个[list]元素带两个参数的for循环 10-3. 文件信息:对包含在变量中的文件列表进行操作 10-4. 在for循环中操作文件 10-5. 在for循环中省略[list] 10-6. 使用命令替换来产生for循环的[list] 10-7. 对于二进制文件的一个grep替换 10-8. 列出系统上的所有用户 10-9. 在目录的所有文件中查找源字串 10-10. 列出目录中所有的符号连接文件 10-11. 将目录中的符号连接文件名保存到一个文件中 10-12. 一个C风格的for循环 10-13. 在batch mode中使用efax 10-14. 简单的while循环 10-15. 另一个while循环 10-16. 多条件的while循环 10-17. C风格的while循环 10-18. until循环 10-19. 嵌套循环 10-20. break和continue命令在循环中的效果 10-21. 多层循环的退出 10-22. 多层循环的continue 10-23. 在实际的任务中使用"continue N" 10-24. 使用case 10-25. 使用case来创建菜单 10-26. 使用命令替换来产生case变量 10-27. 简单字符串匹配 10-28. 检查是否是字母输入 10-29. 用select来创建菜单 10-30. 用函数中select结构来创建菜单 11-1. 一个fork出多个自己实例的脚本 11-2. printf 11-3. 使用read,变量分配 11-4. 当使用一个不带变量参数的read命令时,将会发生什么? 11-5. read命令的多行输入 11-6. 检测方向键 11-7. 通过文件重定向来使用read 11-8. 管道输出到read中的问题 11-9. 修改当前的工作目录 11-10. 用"let"命令来作算术操作. 11-11. 显示eval命令的效果 11-12. 强制登出(log-off) 11-13. 另一个"rot13"的版本 11-14. 在Perl脚本中使用eval命令来强制变量替换 11-15. 使用set来改变脚本的位置参数 11-16. 重新分配位置参数 11-17. Unset一个变量 11-18. 使用export命令传递一个变量到一个内嵌awk的脚本中 11-19. 使用getopts命令来读取传递给脚本的选项/参数. 11-20. "Including"一个数据文件 11-21. 一个没什么用的,source自身的脚本 11-22. exec的效果 11-23. 一个exec自身的脚本 11-24. 在继续处理之前,等待一个进程的结束 11-25. 一个结束自身的脚本. 12-1. 使用ls命令来创建一个烧录CDR的内容列表 12-2. Hello or Good-bye 12-3. 删除当前目录下文件名中包含一些特殊字符(包括空白)的文件.. 12-4. 通过文件的 inode 号来删除文件 12-5. Logfile: 使用 xargs 来监控系统 log 12-6. 把当前目录下的文件拷贝到另一个文件中 12-7. 通过名字Kill进程 12-8. 使用xargs分析单词出现的频率 12-9. 使用 expr 12-10. 使用 date 命令 12-11. 分析单词出现的频率 12-12. 那个文件是脚本? 12-13. 产生10进制随机数 12-14. 使用 tail 命令来监控系统log 12-15. 在一个脚本中模仿 "grep" 的行为 12-16. 在1913年的韦氏词典中查找定义 12-17. 检查列表中单词的正确性 12-18. 转换大写: 把一个文件的内容全部转换为大写. 12-19. 转换小写: 将当前目录下的所有文全部转换为小写. 12-20. Du: DOS 到 UNIX 文本文件的转换. 12-21. rot13: rot13, 弱智加密. 12-22. Generating "Crypto-Quote" Puzzles 12-23. 格式化文件列表. 12-24. 使用 column 来格式化目录列表 12-25. nl: 一个自己计算行号的脚本. 12-26. manview: 查看格式化的man页 12-27. 使用 cpio 来拷贝一个目录树 12-28. 解包一个 rpm 归档文件 12-29. 从 C 文件中去掉注释 12-30. Exploring /usr/X11R6/bin 12-31. 一个"改进过"的 strings 命令 12-32. 在一个脚本中使用 cmp 来比较2个文件. 12-33. basename 和 dirname 12-34. 检查文件完整性 12-35. Uudecod 编码后的文件 12-36. 查找滥用的连接来报告垃圾邮件发送者 12-37. 分析一个垃圾邮件域 12-38. 获得一份股票报价 12-39. 更新 Fedora Core 4 12-40. 使用 ssh 12-41. 一个可以mail自己的脚本 12-42. 按月偿还贷款 12-43. 数制转换 12-44. 使用 "here document" 来调用 bc 12-45. 计算圆周率 12-46. 将10进制数字转换为16进制数字 12-47. 因子分解 12-48. 计算直角三角形的斜边 12-49. 使用 seq 来产生循环参数 12-50. 字母统计 12-51. 使用getopt来分析命令行选项 12-52. 一个拷贝自身的脚本 12-53. 练习dd 12-54. 记录按键 12-55. 安全的删除一个文件 12-56. 文件名产生器 12-57. 将米转换为英里 12-58. 使用 m4 13-1. 设置一个新密码 13-2. 设置一个擦除字符 13-3. 关掉终端对于密码的echo 13-4. 按键检测 13-5. Checking a remote server for identd 13-6. pidof 帮助杀掉一个进程 13-7. 检查一个CD镜像 13-8. 在一个文件中创建文件系统 13-9. 添加一个新的硬盘驱动器 13-10. 使用umask来将输出文件隐藏起来 13-11. killall, 来自于 /etc/rc.d/init.d 14-1. 愚蠢的脚本策略 14-2. 从循环的输出中产生一个变量 14-3. 找anagram(回文构词法, 可以将一个有意义的单词, 变换为1个或多个有意义的单词, 但是还是原来的子母集合) 16-1. 使用exec重定向标准输入 16-2. 使用exec来重定向stdout 16-3. 使用exec在同一脚本中重定向stdin和stdout 16-4. 避免子shell 16-5. while循环的重定向 16-6. 另一种while循环的重定向 16-7. until循环重定向 16-8. for循环重定向 16-9. for循环重定向 loop (将标准输入和标准输出都重定向了) 16-10. 重定向if/then测试结构 16-11. 用于上面例子的"names.data"数据文件 16-12. 记录日志事件 17-1. 广播: 发送消息给每个登录上的用户 17-2. 仿造文件: 创建一个两行的仿造文件 17-3. 使用cat的多行消息 17-4. 带有抑制tab功能的多行消息 17-5. 使用参数替换的here document 17-6. 上传一个文件对到"Sunsite"的incoming目录 17-7. 关闭参数替换 17-8. 一个产生另外一个脚本的脚本 17-9. Here documents与函数 17-10. "匿名" here Document 17-11. 注释掉一段代码块 17-12. 一个自文档化(self-documenting)的脚本 17-13. 在一个文件的开头添加文本 20-1. 子shell中的变量作用域 20-2. 列出用户的配置文件 20-3. 在子shell里进行串行处理 21-1. 在受限的情况下运行脚本 23-1. 简单函数 23-2. 带着参数的函数 23-3. 函数和被传给脚本的命令行参数 23-4. 传递间接引用给函数 23-5. 解除传递给函数的参数引用 23-6. 再次尝试解除传递给函数的参数引用 23-7. 两个数中的最大者 23-8. 把数字转化成罗马数字 23-9. 测试函数最大的返回值 23-10. 比较两个大整数 23-11. 用户名的真实名 23-12. 局部变量的可见范围 23-13. 用局部变量来递归 23-14. 汉诺塔 24-1. 脚本中的别名 24-2. unalias: 设置和删除别名 25-1. 使用"与列表(and list)"来测试命令行参数 25-2. 用"与列表"的另一个命令行参数测试 25-3. "或列表"和"与列表"的结合使用 26-1. 简单的数组用法 26-2. 格式化一首诗 26-3. 多种数组操作 26-4. 用于数组的字符串操作符 26-5. 将脚本的内容传给数组 26-6. 一些数组专用的工具 26-7. 关于空数组和空数组元素 26-8. 初始化数组 26-9. 复制和连接数组 26-10. 关于连接数组的更多信息 26-11. 一位老朋友: 冒泡排序 26-12. 内嵌数组和间接引用 26-13. 复杂数组应用: 埃拉托色尼素数筛子 26-14. 模拟下推的堆栈 26-15. 复杂的数组应用: 列出一种怪异的数学序列 26-16. 模拟二维数组,并使它倾斜 27-1. 利用/dev/tcp 来检修故障 27-2. 搜索与一个PID相关的进程 27-3. 网络连接状态 28-1. 隐藏cookie而不再使用 28-2. 用/dev/zero创建一个交换临时文件 28-3. 创建ramdisk 29-1. 一个错误的脚本 29-2. 丢失关键字(keyword) 29-3. 另一个错误脚本 29-4. 用"assert"测试条件 29-5. 捕捉 exit 29-6. 在Control-C后清除垃圾 29-7. 跟踪变量 29-8. 运行多进程 (在多处理器的机器里) 31-1. 数字和字符串比较是不相等同的 31-2. 子SHELL缺陷 31-3. 把echo的输出用管道输送给read命令 33-1. shell 包装 33-2. 稍微复杂一些的shell包装 33-3. 写到日志文件的shell包装 33-4. 包装awk的脚本 33-5. 另一个包装awk的脚本 33-6. 把Perl嵌入Bash脚本 33-7. Bash 和 Perl 脚本联合使用 33-8. 递归调用自己本身的(无用)脚本 33-9. 递归调用自己本身的(有用)脚本 33-10. 另一个递归调用自己本身的(有用)脚本 33-11. 一个 "彩色的" 地址资料库 33-12. 画盒子 33-13. 显示彩色文本 33-14. "赛马" 游戏 33-15. 返回值技巧 33-16. 整型还是string? 33-17. 传递和返回数组 33-18. anagrams游戏 33-19. 在shell脚本中调用的窗口部件 34-1. 字符串扩展 34-2. 间接变量引用 - 新方法 34-3. 使用间接变量引用的简单数据库应用 34-4. 用数组和其他的小技巧来处理四人随机打牌 A-1. mailformat: Formatting an e-mail message A-2. rn: A simple-minded file rename utility A-3. blank-rename: renames filenames containing blanks A-4. encryptedpw: Uploading to an ftp site, using a locally encrypted password A-5. copy-cd: Copying a data CD A-6. Collatz series A-7. days-between: Calculate number of days between two dates A-8. Make a "dictionary" A-9. Soundex conversion A-10. "Game of Life" A-11. Data file for "Game of Life" A-12. behead: Removing mail and news message headers A-13. ftpget: Downloading files via ftp A-14. password: Generating random 8-character passwords A-15. fifo: Making daily backups, using named pipes A-16. Generating prime numbers using the modulo operator A-17. tree: Displaying a directory tree A-18. string functions: C-like string functions A-19. Directory information A-20. Object-oriented database A-21. Library of hash functions A-22. Colorizing text using hash functions A-23. Mounting USB keychain storage devices A-24. Preserving weblogs A-25. Protecting literal strings A-26. Unprotecting literal strings A-27. Spammer Identification A-28. Spammer Hunt A-29. Making wget easier to use A-30. A "podcasting" script A-31. Basics Reviewed A-32. An expanded cd command C-1. Counting Letter Occurrences K-1. Sample .bashrc file L-1. VIEWDATA.BAT: DOS Batch File L-2. viewdata.sh: Shell Script Conversion of VIEWDATA.BAT P-1. Print the server environment
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xv About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xix ChaPter 1 introduction to Shell Scripting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 About This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2 What Shell Scripting Is . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3 What Shell Scripting Isn’t . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5 Why Shell? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6 The Bourne Shell Family . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7 Why Portable? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8 Why Not? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9 Beyond Portability: Cleanliness and Good Living . . . . . . . . . . . . . . . .10 What’s in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11 Introducing the Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11 Interactive and Noninteractive Usage . . . . . . . . . . . . . . . . . . . . . . . . . .11 Simple Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12 Introducing Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .14 Introducing Quoting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .15 The printf Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .18 What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .20 ChaPter 2 Patterns and regular expressions . . . . . . . . . . . . . . . . . . . . . . . . .21 Shell Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .21 Pattern-Matching Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .22 Character Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .24 Using Shell Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .26 Pathname Expansion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .27 Differences from Shell Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .27 Using Globs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .30 10436fmfinal 7 10/23/08 10:40:25 PM ■CONTENTS viii Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .31 Basic Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .32 Backreferences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .33 Extended Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .34 Common Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .36 Replacements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .36 Using Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .37 Replacing Patterns with Regular Expressions . . . . . . . . . . . . . . . . . . .39 Common Pitfalls of Regular Expressions . . . . . . . . . . . . . . . . . . . . . . .40 What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .41 ChaPter 3 Basic Shell Scripting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .43 Introducing Control Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .43 What Is Truth? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .44 Introducing Conditional Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . .47 Introducing Iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .52 Thinking About Control Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . .55 Introducing Redirection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .56 Understanding File Descriptors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .57 Redirection Using exec . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .61 Introducing Here Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .64 Redirection and Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .66 What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .68 ChaPter 4 Core Shell Features explained . . . . . . . . . . . . . . . . . . . . . . . . . . . . .69 Parsing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .70 Tokens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .70 Words and Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .72 Command Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .73 Shell Quoting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .77 Escaping Characters with a Backslash . . . . . . . . . . . . . . . . . . . . . . . .77 Escaping Characters with Single Quotes . . . . . . . . . . . . . . . . . . . . . . .79 Escaping Characters with Double Quotes . . . . . . . . . . . . . . . . . . . . . .79 Quoting Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .79 10436fmfinal 8 10/23/08 10:40:26 PM ■CONTENTS ix Substitution and Expansion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .80 Substitution and Field Splitting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .80 Understanding Parameter Substitution . . . . . . . . . . . . . . . . . . . . . . . .81 Tilde Expansion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .85 Globbing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .86 What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .87 ChaPter 5 Shells Within Shells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .89 Understanding Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .89 Variables and the Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .90 Manipulating the Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .91 Temporary Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .92 Exploring Subshells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .94 Subshells and External Shells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .94 Command Substitution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .95 Implicit and Explicit Subshells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .97 Modifying the State of the Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .99 Shell Builtins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .99 Shell Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .99 The eval Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .102 The dot ( .) Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .108 Using Shells Within Shells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .109 When to Use an External Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .109 When to Use eval or dot ( .) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .111 When to Use Subshells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .111 When to Use Command Substitution . . . . . . . . . . . . . . . . . . . . . . . . .112 Combinations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .113 What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .115 ChaPter 6 invocation and execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .117 Shell Invocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .117 How UNIX Runs Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .117 Shell Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .119 Using Positional Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .120 Manipulating Parameters for Fun and Profit . . . . . . . . . . . . . . . . . . .122 Shell Startup and Interactive Sessions . . . . . . . . . . . . . . . . . . . . . . . .128 10436fmfinal 9 10/23/08 10:40:26 PM ■CONTENTS x Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .129 More on Jobs and Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .129 Understanding Runtime Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . .137 Debugging Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .142 What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .143 ChaPter 7 Shell Language Portability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .145 More on Portability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .145 Standardization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .146 Bugs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .147 Portability Issues: Welcome to the Club . . . . . . . . . . . . . . . . . . . . . . .147 Common Extensions and Omissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .148 Other Kinds of Expansion and Substitution . . . . . . . . . . . . . . . . . . . .148 Syntax Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .154 Common Omissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .158 Common Shells and Their Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .158 Almquist Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .158 Bourne-Again Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .159 Debian Almquist Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .160 Korn Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .161 Public Domain Korn Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .162 Solaris /usr/xpg4/bin/sh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .163 SVR2 Bourne Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .164 SVR4 Bourne Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .164 Traditional Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .165 Z Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .166 Execution Preambles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .167 Setting Options and Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .168 Picking a Better Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .169 Self-Modifying Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .170 Emulating Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .172 What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .174 10436fmfinal 10 10/23/08 10:40:27 PM ■CONTENTS xi ChaPter 8 Utility Portability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .175 Common Variations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .175 Days of Yore: System V and BSD . . . . . . . . . . . . . . . . . . . . . . . . . . . .176 GNU Arrives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .177 Standardization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .178 busybox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .178 Shell Builtins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .179 Avoiding Unnecessary Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . .180 Relying on Extensions Considered Harmful . . . . . . . . . . . . . . . . . . . .181 Test with More Than One Shell on More Than One System . . . . . .182 Document Your Assumptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .182 Common Utility Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .183 Public Enemy #1: echo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .183 Multiple Different Versions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .184 Archive Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .185 Block Sizes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .188 Other Common Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .188 What to Do When Something Is Unavailable . . . . . . . . . . . . . . . . . . . . . . . .200 Roll Your Own . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .200 Add a Missing Utility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .201 Use Something Else . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .201 Demand a Real System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .201 A Few Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .202 What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .203 ChaPter 9 Bringing it all together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .205 Robustness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .205 Handling Failure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .205 Temporary Files and Cleanup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .208 Handling Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .210 Startup Files and Environment Variables . . . . . . . . . . . . . . . . . . . . . .211 Documentation and Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .212 Degrade Gracefully . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .214 Specify, and Test For, Requirements . . . . . . . . . . . . . . . . . . . . . . . . .216 Scripts That Write Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .216 Building a Script for a Specific Target . . . . . . . . . . . . . . . . . . . . . . . .217 Mixing with Other Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .218 What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .218 10436fmfinal 11 10/23/08 10:40:27 PM ■CONTENTS xii ChaPter 10 Shell Script design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .219 Do One Thing Well . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .219 Separate Functionality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .220 Isolate Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .220 Be Cooperative . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .221 Filters, File Manipulation, and Program Manipulation . . . . . . . . . . .221 Command-Line Options and Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . .224 Designing Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .226 Options and Inputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .228 Set Reasonable Limits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .229 Define Your Functional Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .230 Define Your Target Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .230 Case Study: pids . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .232 What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .235 ChaPter 11 Mixing and Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .237 Mixing Quoting Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .237 Embedding Shell Scripts in Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .239 Shell and make . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .239 Shell and C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .241 Embedding Code in Shell Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .242 Shell and sed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .242 Shell and awk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .246 Utilities and Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .252 What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .253 aPPeNdix a the Shell Command Language . . . . . . . . . . . . . . . . . . . . . . . . . . . .255 2 . Shell Command Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .255 2 .1 Shell Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .255 2 .2 Quoting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .256 2 .3 Token Recognition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .258 2 .4 Reserved Words . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .260 2 .5 Parameters and Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .260 2 .6 Word Expansions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .264 2 .7 Redirection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .271 2 .8 Exit Status and Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .275 2 .9 Shell Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .276 10436fmfinal 12 10/23/08 10:40:28 PM ■CONTENTS xiii 2 .10 Shell Grammar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .285 2 .11 Signals and Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . .292 2 .12 Shell Execution Environment . . . . . . . . . . . . . . . . . . . . . . . . . . .293 2 .13 Pattern Matching Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .294 2 .14 Special Built- In Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .296 aPPeNdix B the sh Utility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .297 Name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .297 Synopsis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .297 Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .297 Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .298 Operands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .298 Stdin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .299 Input Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .300 Environment Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .300 Asynchronous Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .303 Stdout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .303 Stderr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .303 Output Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .303 Extended Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .303 Command History List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .303 Command Line Editing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .303 Command Line Editing (vi- mode) . . . . . . . . . . . . . . . . . . . . . . . . . . . .304 vi Line Editing Insert Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .304 vi Line Editing Command Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .305 Exit Status . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .314 Consequences Of Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .314 Application Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .314 Rationale . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .316 Future Directions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .318 See Also . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .318 Change History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .318 Issue 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .318 Issue 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .318 10436fmfinal 13 10/23/08 10:40:28 PM ■CONTENTS xiv aPPeNdix C regular expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .319 9 . Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .319 9 .1 Regular Expression Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . .320 9 .2 Regular Expression General Requirements . . . . . . . . . . . . . . . .321 9 .3 Basic Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .322 9 .4 Extended Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . .327 9 .5 Regular Expression Grammar . . . . . . . . . . . . . . . . . . . . . . . . . . . .330 iNdex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .339
Awk入门教程。作者 Bruce Barnett 注:英文版。以下是目录 Why learn AWK? Basic Structure Executing an AWK script Which shell to use with AWK? Dynamic Variables The Essential Syntax of AWK Arithmetic Expressions Unary arithmetic operators The Autoincrement and Autodecrement Operators Assignment Operators Conditional expressions Regular Expressions And/Or/Not Commands AWK Built-in Variables FS - The Input Field Separator Variable OFS - The Output Field Separator Variable NF - The Number of Fields Variable NR - The Number of Records Variable RS - The Record Separator Variable ORS - The Output Record Separator Variable FILENAME - The Current Filename Variable Associative Arrays Multi-dimensional Arrays Example of using AWK's Associative Arrays Output of the script Picture Perfect PRINTF Output PRINTF - formatting output Escape Sequences Format Specifiers Width - specifying minimum field size Left Justification The Field Precision Value Explicit File output AWK Numerical Functions Trigonometric Functions Exponents, logs and square roots Truncating Integers "Random Numbers The Lotto script String Functions The Length function The Index Function The Substr function GAWK's Tolower and Toupper function The Split function NAWK's string functions The Match function The System function The Getline function The systime function The Strftime function User Defined Functions AWK patterns Formatting AWK programs Environment Variables ARGC - Number or arguments (NAWK/GAWK) ARGV - Array of arguments (NAWK/GAWK) ARGIND - Argument Index (GAWK only) FNR (NAWK/GAWK) OFMT (NAWK/GAWK) RSTART, RLENGTH and match (NAWK/GAWK) SUBSEP - Multi-dimensional array separator (NAWK/GAWK) ENVIRON - environment variables (GAWK only) IGNORECASE (GAWK only) CONVFMT - conversion format (GAWK only) ERRNO - system errors (GAWK only) FIELDWIDTHS - fixed width fields (GAWK only) AWK, NAWK, GAWK, or PERL
Part I Basic Concepts 1 The Nature of Hardware and Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.1 Introducing Hardware/Software Codesign .. . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.1.1 Hardware .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.1.2 Software .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1.1.3 Hardware and Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 1.1.4 Defining Hardware/Software Codesign . . . . . . . . . . . . . . . . . . . . . 11 1.2 The Quest for Energy Efficiency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 1.2.1 Relative Performance .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 1.2.2 Energy Efficiency .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 1.3 The Driving Factors in Hardware/Software Codesign.. . . . . . . . . . . . . . . 15 1.4 The Hardware–Software Codesign Space. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 1.4.1 The Platform Design Space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 1.4.2 Application Mapping .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 1.5 The Dualism of Hardware Design and Software Design .. . . . . . . . . . . . 20 1.6 More on Modeling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 1.6.1 Abstraction Levels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 1.7 Concurrency and Parallelism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 1.8 Summary.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 1.9 Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 1.10 Problems .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 2 Data Flow Modeling and Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 2.1 The Need for Concurrent Models: An Example . . . . . . . . . . . . . . . . . . . . . . 33 2.1.1 Tokens, Actors, and Queues. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 2.1.2 Firing Rates, Firing Rules, and Schedules. . . . . . . . . . . . . . . . . . . 38 2.1.3 Synchronous Data Flow Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 2.1.4 SDF Graphs are Determinate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 2.2 Analyzing Synchronous Data Flow Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 2.2.1 Deriving Periodic Admissible Sequential Schedules . . . . . . . 41 2.2.2 Example: Euclid’s Algorithm as an SDF Graph . . . . . . . . . . . . 44 2.3 Control Flow Modeling and the Limitations of Data Flow Models . . 45 2.3.1 Emulating Control Flow with SDF Semantics . . . . . . . . . . . . . . 46 2.3.2 Extending SDF Semantics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 2.4 Software Implementation of Data Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 2.4.1 Converting Queues and Actors into Software .. . . . . . . . . . . . . . 48 2.4.2 Sequential Targets with Dynamic Schedule .. . . . . . . . . . . . . . . . 51 2.4.3 Sequential Targets with Static Schedule . . . . . . . . . . . . . . . . . . . . . 57 2.5 Hardware Implementation of Data Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 2.5.1 Single-Rate SDF Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 2.5.2 Pipelining . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 2.5.3 Multirate Expansion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 2.6 Summary.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 2.7 Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 2.8 Problems .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 3 Analysis of Control Flow and Data Flow. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 3.1 Data and Control Edges of a C Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 3.2 Implementing Data and Control Edges. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 3.3 Contruction of the Control Flow Graph .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 3.4 Construction of the Data Flow Graph . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 3.5 Application: Translating C to Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 3.5.1 Designing the Datapath. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 3.5.2 Designing the Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 3.6 Single-Assignment Programs .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 3.7 Summary.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 3.8 Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 3.9 Problems .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Part II The Design Space of Custom Architectures 4 Finite State Machine with Datapath.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 4.1 Cycle-Based Bit-Parallel Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 4.1.1 Wires and Registers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 4.1.2 Precision and Sign . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 4.1.3 Hardware Mapping of Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . 99 4.2 Hardware Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .102 4.3 Finite State Machines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .104 4.4 Finite State Machines with Datapath .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .107 4.4.1 Modeling .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .107 4.4.2 An FSMD is Not Unique . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .111 4.4.3 Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .113 4.5 Simulation and RTL Synthesis of FSMD . . . . . . . . . . . . . . . . . . . . . . . . . . . . .115 4.5.1 Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .115 4.5.2 Code Generation and Synthesis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .117 4.6 Proper FSMD. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .117 4.7 Language Mapping for FSMD by Example. . . . . . . . . . . . . . . . . . . . . . . . . . .119 4.7.1 GCD in GEZEL. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .119 4.7.2 GCD in Verilog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .120 4.7.3 GCD in VHDL. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .122 4.7.4 GCD in SystemC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .124 4.8 Summary.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .126 4.9 Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .126 4.10 Problems .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .127 5 Microprogrammed Architectures.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .133 5.1 Limitations of Finite State Machines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .133 5.1.1 State Explosion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .133 5.1.2 Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .134 5.1.3 Runtime Flexibility .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .135 5.2 Microprogrammed Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .136 5.3 Microinstruction Encoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .137 5.3.1 Jump Field . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .137 5.3.2 Command Field . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .139 5.4 The Microprogrammed Datapath . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .141 5.4.1 Datapath Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .141 5.4.2 Writing Microprograms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .142 5.5 Implementing a MicroprogrammedMachine . . . . . . . . . . . . . . . . . . . . . . . . .144 5.5.1 MicroinstructionWord Definition . . . . . . . . . . . . . . . . . . . . . . . . . . .144 5.6 Microprogram Interpreters .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .151 5.7 Microprogram Pipelining . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .155 5.7.1 Microinstruction Register . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .156 5.7.2 Datapath Condition-Code Register . . . . . . . . . . . . . . . . . . . . . . . . . .157 5.7.3 Pipelined Next-Address Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .158 5.8 Picoblaze: A ContemporaryMicroprogram Controller.. . . . . . . . . . . . . .158 5.9 Summary.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .160 5.10 Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .160 5.11 Problems .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .161 6 General-Purpose Embedded Cores . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .165 6.1 Processors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .165 6.1.1 The Toolchain of a Typical Microprocessor .. . . . . . . . . . . . . . . .166 6.1.2 From C to Assembly Instructions .. . . . . . . . . . . . . . . . . . . . . . . . . . .167 6.1.3 Simulating a C Program Executing on a Microprocessor . .170 6.2 The RISC Pipeline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .173 6.2.1 Control Hazards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .174 6.2.2 Data Hazards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .176 6.2.3 Structural Hazards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .177 6.3 Program Organization .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .178 6.3.1 Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .179 6.3.2 Variables in the Memory Hierarchy . . . . . . . . . . . . . . . . . . . . . . . . .180 6.3.3 Function Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .183 6.3.4 Program Layout.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .186 6.4 Analyzing the Quality of Compiled Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . .190 6.4.1 Analysis Based on Static Assembly Code . . . . . . . . . . . . . . . . . . .190 6.4.2 Analysis Based on Execution of Object Code. . . . . . . . . . . . . . .194 6.5 Summary.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .198 6.6 Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .198 6.7 Problems .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .199 7 SystemOnChip. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .205 7.1 The System-on-Chip Concept . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .205 7.1.1 The Cast of Players . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .206 7.1.2 SoC Interfaces for Custom Hardware . . . . . . . . . . . . . . . . . . . . . . .207 7.2 Four Design Principles in SoC Architecture . . . . . . . . . . . . . . . . . . . . . . . . . .209 7.2.1 Heterogeneous and Distributed Data Processing. . . . . . . . . . . .209 7.2.2 Heterogeneous and Distributed Communications.. . . . . . . . . .210 7.2.3 Heterogeneous and Distributed Storage . . . . . . . . . . . . . . . . . . . . .211 7.2.4 Hierarchical Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .214 7.3 Example: Portable Multimedia System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .215 7.4 SoC Modeling in GEZEL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .217 7.4.1 An SoC with a StrongARM Core . . . . . . . . . . . . . . . . . . . . . . . . . . . .218 7.4.2 Ping-Pong Buffer with an 8051 .. . . . . . . . . . . . . . . . . . . . . . . . . . . . .221 7.5 Summary.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .225 7.6 Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .225 7.7 Problems .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .226 Part III Hardware/Software Interfaces 8 On-Chip Busses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .231 8.1 Connecting Hardware and Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .231 8.2 On-Chip Bus Systems. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .232 8.2.1 Some Existing On-Chip Bus Systems . . . . . . . . . . . . . . . . . . . . . . .232 8.2.2 Bus Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .233 8.2.3 Bus Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .234 8.2.4 Bus Timing Diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .235 8.3 Bus Transfers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .237 8.3.1 Simple Read and Write Transfers . . . . . . . . . . . . . . . . . . . . . . . . . . . .237 8.3.2 Transfer Sizing and Endianess . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .238 8.3.3 Improved Bus Transfers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .242 8.4 Multimaster Bus Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .245 8.4.1 Bus Priority . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .246 8.4.2 Bus Locking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .248 8.5 On-Chip Networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .250 8.6 Summary.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .253 8.7 Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .254 8.8 Problems .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .254 9 Hardware/Software Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .259 9.1 The Hardware/Software Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .259 9.2 Synchronization Schemes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .260 9.2.1 Synchronization Concepts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .260 9.2.2 Semaphore . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .262 9.2.3 One-Way and Two-Way Handshake . . . . . . . . . . . . . . . . . . . . . . . . .265 9.2.4 Blocking and Nonblocking Data-Transfer.. . . . . . . . . . . . . . . . . .267 9.3 Memory-Mapped Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .268 9.3.1 The Memory-Mapped Register . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .268 9.3.2 Mailboxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .271 9.3.3 First-In First-Out Queues. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .272 9.3.4 Slave and Master Handshakes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .273 9.3.5 Shared Memory .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .274 9.3.6 GEZEL Modeling of Memory-Mapped Interfaces.. . . . . . . . .275 9.4 Coprocessor Interfaces .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .279 9.4.1 Tight and Loose Coupling.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .281 9.4.2 The Fast Simplex Link . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .282 9.4.3 The LEON-3 Floating Point Coprocessor Interface . . . . . . . .284 9.5 Custom-Instruction Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .286 9.5.1 ASIP Design Flow. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .287 9.5.2 Example: Endianess Byte-Ordering Processor . . . . . . . . . . . . . .288 9.5.3 Finding Good ASIP Instructions .. . . . . . . . . . . . . . . . . . . . . . . . . . . .293 9.6 Summary.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .297 9.7 Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .297 9.8 Problems .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .298 10 Coprocessor Control Shell Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .303 10.1 The Coprocessor Control Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .303 10.1.1 Functions of the Coprocessor Control Shell. . . . . . . . . . . . . . . . .303 10.1.2 Layout of the Coprocessor Control Shell . . . . . . . . . . . . . . . . . . . .305 10.1.3 Communication-Constrained vs. Computation-Constrained Coprocessors . . . . . . . . . . . . . . . . . . . .306 10.2 Data Design. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .308 10.2.1 Flexible Addressing Mechanisms. . . . . . . . . . . . . . . . . . . . . . . . . . . .308 10.2.2 Multiplexing and Masking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .308 10.3 Control Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .310 10.3.1 Hierarchical Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .311 10.3.2 Control of Internal Pipelining . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .313 10.4 Programmer’sModel = Control Design + Data Design . . . . . . . . . . . . . .317 10.4.1 Address Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .317 10.4.2 Instruction Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .318 10.5 Example: AES Encryption Coprocessor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .319 10.5.1 Control Shell Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .320 10.5.2 Programmer’sModel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .320 10.5.3 Software Driver Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .323 10.5.4 Control Shell Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .324 10.5.5 System Performance Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . . .327 10.6 Summary.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .329 10.7 Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .329 10.8 Problems .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .330 Part IV Applications 11 Trivium Crypto-Coprocessor.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .337 11.1 The Trivium Stream Cipher Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .337 11.1.1 Stream Ciphers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .337 11.1.2 Trivium.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .339 11.1.3 Hardware Mapping of Trivium . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .340 11.1.4 A Hardware Testbench for Trivium. . . . . . . . . . . . . . . . . . . . . . . . . .344 11.2 Trivium for 8-bit Platforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .344 11.2.1 Overall Design of the 8051 Coprocessor . . . . . . . . . . . . . . . . . . . .345 11.2.2 Hardware Platform of the 8051 Coprocessor.. . . . . . . . . . . . . . .346 11.2.3 Software Driver for 8051 .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .350 11.3 Trivium for 32-bit Platforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .354 11.3.1 Hardware Platform Using Memory-mapped Interfaces.. . . .355 11.3.2 Software Driver Using Memory-mapped Interfaces . . . . . . . .358 11.3.3 Hardware Platform Using a Custom-Instruction Interface .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .362 11.3.4 Software Driver for a Custom-Instruction Interface . . . . . . . .364 11.4 Summary.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .366 11.5 Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .367 11.6 Problems .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .367 12 CORDIC Coprocessor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .369 12.1 The Coordinate Rotation Digital Computer Algorithm . . . . . . . . . . . . . .369 12.1.1 The Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .369 12.1.2 Reference Implementation in C. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .371 12.2 A Hardware Coprocessor for CORDIC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .373 12.2.1 A CORDIC Kernel in Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . .373 12.2.2 A Control Shell for Fast-Simplex-Link Coprocessors . . . . . .376 12.3 An FPGA Prototype of the CORDIC Coprocessor . . . . . . . . . . . . . . . . . . .379 12.4 Handling Large Amounts of Rotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .382 12.5 Summary.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .387 12.6 Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .387 12.7 Problems .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .388
VIVADO经常使用TCL脚本对FPGA进行调试,通过JTAG转AXI对内部模块进行控制,但是TCL语言书籍比较少,这边是一个英文的TCL语言学习书籍. Preface 1 Chapter 1: The Tcl Shell 5 Introduction 5 The Tcl shell 6 Writing to the Tcl console 7 Mathematical expressions 8 Tcl expr operands 8 Mathematical functions 11 Computing mathematical expressions 12 Referencing files in Tcl 13 Variables 15 Command line arguments 17 Chapter 2: Using the Building Blocks Control Constructs 21 Introduction 21 Controlling flow with the if statement 23 Looping with for 24 Looping with foreach 25 Looping with while 26 Continuing a procedure 27 Breaking out of a procedure 28 Nested looping 29 Chapter 3: Error Handling 31 Introduction 31 Using the catch command 32 Using the eval command 34 Using the error command 35 Error handling procedure 36 Chapter 4: Handling String Expressions 39 Introduction 40 Appending to a string 41 Formatting a string 42 Matching a regular expression within a string 44 Performing character substitution on a string 46 Parsing a string using conversion specifiers 47 Determining the length of a string 49 Comparing strings 50 Comparing a string of characters 51 Locating the first instance of a character 52 Locating the index of a character 53 Determining the class of a string 54 Locating the last instance of a string 56 Determining the size of a string 57 Replacing values within a string 57 Locating a pattern within a string 58 Returning a range of characters from a string 59 Creating a string of repeated characters 60 Replacing ranges of characters contained within a string 60 Creating a reverse string 61 Converting a string to lowercase 62 Converting a string to title 62 Converting a string to uppercase 63 Trimming a string 64 Trimming leading whitespace 64 Trimming trailing whitespace 65 Locating the word end 65 Locating the word start 66 Performing variable substitution 67 Chapter 5: Expanding String Functionality Using Lists 69 Introduction 70 Creating a list 70 Joining two lists 71 Joining list elements 72 Appending list elements 73 Assigning list elements to variables 73 Retrieving an element from a list 74 Inserting elements into a list 75 Determining the number of elements 75 Getting a list element 76 Repeating elements 77 Replacing elements 77 Reversing elements 78 Searching a list 79 Editing a list 81 Sorting a list 82 Splitting a string into a list 83 Chapter 6: The Tcl Dictionary 85 Introduction 85 Creating a dictionary 86 Appending to a dictionary 87 Determining if a key exists 88 Filtering a dictionary 88 Searching a dictionary 90 Getting a record 91 Incrementing a value 91 Getting the dictionary structure 92 Getting a list of keys 93 Appending to an existing record 94 Merging two dictionaries 94 Creating a blank dictionary structure 95 Updating variables from a dictionary 96 Determining the size of a dictionary 96 Getting all records 97 Assigning values 97 Chapter 7: File Operations 99 Introduction 99 Opening a file 100 Configuring a file 102 Opening a command pipeline 104 Writing a file 106 Reading a file 106 Closing a file 107 File handling 108 Chapter 8: Tk GUI Programming with Tcl/Tk 111 Introduction 111 Creating a widget 113 Writing to the console 115 Setting the attributes of the window through window manager 116 Creating an additional window 117 Destroying a window 119 Creating a custom dialog 121 Chapter 9: Configuring and Controlling Tk Widgets 123 Introduction 123 Creating a frame widget 124 Creating a label widget 126 Creating an entry widget 128 Creating a button widget 130 Creating a listbox widget 133 Creating an image 139 Creating a simple form 140 Chapter 10: Geometry Management 143 Introduction 143 Controlling layout with the pack command 144 Controlling layout with the grid command 147 Combining pack and grid 151 Creating an address book interface 152 Chapter 11: Using Tcl Built-in Dialog Windows 157 Introduction 157 Displaying a message box 158 Displaying a confirmation dialog 159 Displaying the color picker 161 Displaying the directory dialog 162 Displaying the file selection dialog 164 Selecting a directory and file 166 Chapter 12: Creating and Managing Menus 169 Introduction 169 Creating a menu 170 Adding menu buttons 175 Displaying a pop-up menu 178 Data entry application 180 Chapter 13: Creating the Address Book Application 183 Introduction 183 Creating the Address Book application 184 Adding a record 188 Navigating records 191 Deleting a record 192 Finding a record 195 Full listing 196

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值