Shell脚本编程入门指南

需积分: 49 13 下载量 87 浏览量 更新于2024-07-28 2 收藏 564KB PDF 举报
"这篇文章主要介绍了Shell脚本编程的基础知识,包括Shell命令行的运行规则、编写、修改权限和执行Shell程序的步骤,以及如何在Shell程序中使用参数、变量、表达式、循环和条件结构、函数和调用其他Shell程序。" 在Linux环境中,Shell脚本是一种强大的工具,用于自动化日常任务和管理系统。`Shell`是用户与操作系统交互的接口,而`Shell脚本`则是由一系列Linux命令和实用程序组成的文本文件,这些命令按顺序执行,形成一个可执行的程序。 12-1 Shell命令行书写规则 在Shell命令行中,你可以在一个命令行上运行多个命令,通过分号(;)将它们分隔开。例如,`lsa -l; free; df`这条命令会依次执行`lsa -l`、`free`和`df`这三个命令。如果命令太长,可以使用反斜线(\)进行续行,如示例中的`echo`命令。 12-2 编写/修改权限及执行Shell程序的步骤 编写Shell程序通常涉及以下几个步骤: - **编辑Shell程序**:使用文本编辑器(如`vi`)创建包含命令的文件。例如,上述示例中创建了一个名为`date`的文件,包含了打印当前日期和时间的命令。文件的第一行`#!/bin/sh`被称为`Shebang`,指示系统使用哪个Shell解释器来执行脚本。 - **建立可执行程序**:创建好脚本后,需要赋予它执行权限。使用`chmod +x date`命令可以添加执行权限,使得文件能够作为程序运行。 - **执行Shell程序**:有多种方式运行Shell脚本,包括在脚本所在目录使用`./date`,通过绝对路径或相对路径调用,或者通过`bash date.sh`的方式指定Shell解释器运行。 12-2-1 编辑Shell程序时,需要注意`echo`命令中如果要嵌入其他命令,如`date`,需在其前加上`&`符号,表示在后台执行。 12-2-2 给文件添加执行权限后,脚本才能被运行。 12-2-3 执行Shell程序的三种方法包括直接在脚本所在的目录下使用`.`(当前目录)和文件名,通过绝对或相对路径,以及通过指定Shell解释器(如`bash scriptname.sh`)来运行。 在Shell脚本中,还可以使用参数、变量、表达式比较、循环结构(如`for`、`while`)和条件结构(如`if`、`else`)。此外,编写函数和调用其他Shell程序可以提高代码的复用性和组织性。 Shell脚本编程是Linux系统管理中的重要技能,通过学习和掌握这些基础概念,初学者可以逐步构建起自己的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).