Bourne Shell与Shell编程入门教程

需积分: 18 15 下载量 120 浏览量 更新于2024-08-01 收藏 563KB PDF 举报
"这篇文档是关于Bourne Shell和shell编程的实例教程,由大连理工大学LINUX选修课讲义整理而成,作者何斌武。文档涵盖了Bourne Shell的基础知识、shell编程以及如何定制shell环境。" 在UNIX和类UNIX系统中,Bourne Shell是一种重要的命令解释器,由Stephen Bourne在Bell实验室开发。它是许多其他shell(如bash)的基础,提供了丰富的命令行交互和脚本编写功能。 **Bourne Shell基础:** 1. **环境** - Shell的环境设置通过`/etc/passwd`文件来管理用户账户,用户的个人环境配置通常存储在`$HOME/.profile`文件中。提示符一般显示为`$`,但可以通过环境变量进行自定义。 2. **选项** - Shell有多个运行选项,例如: - `-n`:仅检查脚本的语法,不执行。 - `-x`:启用命令追踪,打印执行的每个命令。 - `-i`:交互模式,允许用户与shell交互。 - `-e`:如果命令失败,立即停止执行。 - `-v`:显示shell输入的行号,用于调试。 **特殊字符** - Shell中有一些特殊字符,如`$`用于引用变量,`#`用于注释,`;`用于分隔命令,`&`用于后台执行等。 **shell变量** - 包括: - **用户定义变量**:用户可以自由创建和修改。 - **环境变量**:全局变量,对所有子进程可见,如`PATH`、`HOME`。 - **位置变量(shell参数)**:`$0`表示脚本本身,`$1`到`$9`表示传递给脚本的参数。 **shell脚本编程** - 包括: - **条件测试**:用`if`语句进行条件判断,可以测试文件属性、数值比较等。 - **循环及重复控制**:使用`for`、`while`和`until`等循环结构。 **shell定制**: - 用户可以通过修改`/etc/profile`和`$HOME/.bashrc`等文件来定制shell启动时的行为。 - 可以通过`alias`命令创建命令别名,简化常用命令的输入。 - 受限制的shell (`sh -r` 或 `/bin/rsh`) 限制了某些操作,如改变目录、修改`PATH`或进行输出重定向,常用于提高安全性。 文档中还提到了其他高级主题,如函数定义、错误处理和脚本调试技巧,这些都是shell编程中非常重要的部分。通过学习和实践这些内容,用户可以更高效地利用shell来自动化任务和编写复杂的脚本。
2018-09-23 上传
Shell脚本高级编程教程,希望对你有所帮助。 Example 10-23. Using continue N in an actual task: 1 # Albert Reiner gives an example of how to use "continue N": 2 # --------------------------------------------------------- 3 4 # Suppose I have a large number of jobs that need to be run, with 5 #+ any data that is to be treated in files of a given name pattern in a 6 #+ directory. There are several machines that access this directory, and 7 #+ I want to distribute the work over these different boxen. Then I 8 #+ usually nohup something like the following on every box: 9 10 while true 11 do 12 for n in .iso.* 13 do 14 [ "$n" = ".iso.opts" ] && continue 15 beta=${n#.iso.} 16 [ -r .Iso.$beta ] && continue 17 [ -r .lock.$beta ] && sleep 10 && continue 18 lockfile -r0 .lock.$beta || continue 19 echo -n "$beta: " `date` 20 run-isotherm $beta 21 date 22 ls -alF .Iso.$beta 23 [ -r .Iso.$beta ] && rm -f .lock.$beta 24 continue 2 25 done 26 break 27 done 28 29 # The details, in particular the sleep N, are particular to my 30 #+ application, but the general pattern is: 31 32 while true 33 do 34 for job in {pattern} 35 do 36 {job already done or running} && continue 37 {mark job as running, do job, mark job as done} 38 continue 2 39 done 40 break # Or something like `sleep 600' to avoid termination. 41 done 42 43 # This way the script will stop only when there are no more jobs to do 44 #+ (including jobs that were added during runtime). Through the use 45 #+ of appropriate lockfiles it can be run on several machines 46 #+ concurrently without duplication of calculations [which run a couple 47 #+ of hours in my case, so I really want to avoid this]. Also, as search 48 #+ always starts again from the beginning, one can encode priorities in 49 #+ the file names. Of course, one could also do this without `continue 2', 50 #+ but then one would have to actually check whether or not some job 51 #+ was done (so that we should immediately look for the next job) or not 52 #+ (in which case we terminate or sleep for a long time before checking 53 #+ for a new job).