1. Linux 脚本编写基础YrqLinux联盟
1.1 语法基本介绍YrqLinux联盟
1.1.1 开头YrqLinux联盟
程序必须以下面的行开始(必须方在文件的第一行): YrqLinux联盟
#!/bin/sh YrqLinux联盟
符号#!用来告诉系统它后面的参数是用来执行该文件的程序。在这个例子中我们使用/bin/sh来执行程序。 YrqLinux联盟
当编辑好脚本时,如果要执行该脚本,还必须使其可执行。 YrqLinux联盟
要使脚本可执行:YrqLinux联盟
编译 chmod +x filename 这样才能用./filename 来运行
1.1.2 注释 YrqLinux联盟
在进行shell编程时,以#开头的句子表示注释,直到这一行的结束。我们真诚地建议您在程序中使用注释。YrqLinux联盟
如果您使用了注释,那么即使相当长的时间内没有使用该脚本,您也能在很短的时间内明白该脚本的作用YrqLinux联盟
及工作原理。
1.1.3 变量 YrqLinux联盟
在其他编程语言中您必须使用变量。在shell编程中,所有的变量都由字符串组成,并且您不需要对变量YrqLinux联盟
进行声明。要赋值给一个变量,您可以这样写: YrqLinux联盟
#!/bin/sh YrqLinux联盟
#对变量赋值: YrqLinux联盟
a="hello world" YrqLinux联盟
# 现在打印变量a的内容: YrqLinux联盟
echo "A is:" YrqLinux联盟
echo $a YrqLinux联盟
有时候变量名很容易与其他文字混淆,比如: YrqLinux联盟
num=2 YrqLinux联盟
echo "this is the $numnd" YrqLinux联盟
这并不会打印出"this is the 2nd",而仅仅打印"this is the ",因为shell会去搜索变量numnd的值,YrqLinux联盟
但是这个变量时没有值的。可以使用花括号来告诉shell我们要打印的是num变量: YrqLinux联盟
num=2 YrqLinux联盟
echo "this is the ${num}nd" YrqLinux联盟
这将打印: this is the 2nd
1.1.4 环境变量YrqLinux联盟
由export关键字处理过的变量叫做环境变量。我们不对环境变量进行讨论,因为通常情况下仅仅在登录YrqLinux联盟
脚本中使用环境变量。
1.1.5 Shell命令和流程控制YrqLinux联盟
在shell脚本中可以使用三类命令: YrqLinux联盟
1)Unix 命令: YrqLinux联盟
虽然在shell脚本中可以使用任意的unix命令,但是还是由一些相对更常用的命令。这些命令通常是用来YrqLinux联盟
进行文件和文字操作的。 YrqLinux联盟
常用命令语法及功能 YrqLinux联盟
echo "some text": 将文字内容打印在屏幕上 YrqLinux联盟
ls: 文件列表 YrqLinux联盟
wc –l filewc -w filewc -c file: 计算文件行数计算文件中的单词数计算文件中的字符数 YrqLinux联盟
cp sourcefile destfile: 文件拷贝 YrqLinux联盟
mv oldname newname : 重命名文件或移动文件 YrqLinux联盟
rm file: 删除文件 YrqLinux联盟
grep 'pattern' file: 在文件内搜索字符串比如:grep 'searchstring' file.txt YrqLinux联盟
cut -b colnum file: 指定欲显示的文件内容范围,并将它们输出到标准输出设备比如:输出YrqLinux联盟
每行第5个到第9个字符cut -b5-9 file.txt千万不要和cat命令混淆,YrqLinux联盟
这是两个完全不同的命令 YrqLinux联盟
cat file.txt: 输出文件内容到标准输出设备(屏幕)上 YrqLinux联盟
file somefile: 得到文件类型 YrqLinux联盟
read var: 提示用户输入,并将输入赋值给变量 YrqLinux联盟
sort file.txt: 对file.txt文件中的行进行排序 YrqLinux联盟
uniq: 删除文本文件中出现的行列比如: sort file.txt | uniq YrqLinux联盟
expr: 进行数学运算Example: add 2 and 3expr 2 "+" 3 YrqLinux联盟
find: 搜索文件比如:根据文件名搜索find . -name filename -print YrqLinux联盟
tee: 将数据输出到标准输出设备(屏幕) 和文件比如:somecommand | tee outfile YrqLinux联盟
basename file: 返回不包含路径的文件名比如: basename /bin/tux将返回 tux YrqLinux联盟
dirname file: 返回文件所在路径比如:dirname /bin/tux将返回 /bin YrqLinux联盟
head file: 打印文本文件开头几行 YrqLinux联盟
tail file : 打印文本文件末尾几行 YrqLinux联盟
sed: Sed是一个基本的查找替换程序。可以从标准输入(比如命令管道)读入文本,并将YrqLinux联盟
结果输出到标准输出(屏幕)。该命令采用正则表达式(见参考)进行搜索。 YrqLinux联盟
不要和shell中的通配符相混淆。比如:将linuxfocus 替换为 YrqLinux联盟
LinuxFocus :cat text.file | sed 's/linuxfocus/LinuxFocus/' > newtext.file YrqLinux联盟
awk: awk 用来从文本文件中提取字段。缺省地,字段分割符是空格,可以使用-F指定其他分割符。YrqLinux联盟
cat file.txt | awk -F, '{print $1 "," $3 }'这里我们使用,作为字段分割符,同时打印YrqLinux联盟
第一个和第三个字段。如果该文件内容如下: Adam Bor, 34, IndiaKerry Miller, 22, USAYrqLinux联盟
命令输出结果为:Adam Bor, IndiaKerry Miller, USA YrqLinux联盟
2) 概念: 管道, 重定向和 backtick YrqLinux联盟
这些不是系统命令,但是他们真的很重要。
管道 (|) 将一个命令的输出作为另外一个命令的输入。 YrqLinux联盟
grep "hello" file.txt | wc -l YrqLinux联盟
在file.txt中搜索包含有”hello”的行并计算其行数。 YrqLinux联盟
在这里grep命令的输出作为wc命令的输入。当然您可以使用多个命令。 YrqLinux联盟
重定向:将命令的结果输出到文件,而不是标准输出(屏幕)。
> 写入文件并覆盖旧文件 YrqLinux联盟
>> 加到文件的尾部,保留旧文件内容。
反短斜线 YrqLinux联盟
使用反短斜线可以将一个命令的输出作为另外一个命令的一个命令行参数。YrqLinux联盟
命令: YrqLinux联盟
find . -mtime -1 -type f -print YrqLinux联盟
用来查找过去24小时(-mtime –2则表示过去48小时)内修改过的文件。如果您YrqLinux联盟
想将所有查找到的文件打一个包,则可以使用以下脚本: YrqLinux联盟
#!/bin/sh YrqLinux联盟
# The ticks are backticks (`) not normal quotes ('): YrqLinux联盟
tar -zcvf lastmod.tar.gz `find . -mtime -1 -type f -print` YrqLinux联盟
3) 流程控制 YrqLinux联盟
1.if YrqLinux联盟
"if" 表达式 如果条件为真则执行then后面的部分: YrqLinux联盟
if ....; then YrqLinux联盟
.... YrqLinux联盟
elif ....; then YrqLinux联盟
.... YrqLinux联盟
else YrqLinux联盟
.... YrqLinux联盟
fiYrqLinux联盟
大多数情况下,可以使用测试命令来对条件进行测试。比如可以比较字符串、判断文件YrqLinux联盟
是否存在及是否可读等等… YrqLinux联盟
通常用" [ ] "来表示条件测试。注意这里的空格很重要。要确保方括号的空格。 YrqLinux联盟
[ -f "somefile" ] :判断是否是一个文件 YrqLinux联盟
[ -x "/bin/ls" ] :判断/bin/ls是否存在并有可执行权限 YrqLinux联盟
[ -n "$var" ] :判断$var变量是否有值 YrqLinux联盟
[ "$a" = "$b" ] :判断$a和$b是否相等 YrqLinux联盟
执行man test可以查看所有测试表达式可以比较和判断的类型。 YrqLinux联盟
直接执行以下脚本: YrqLinux联盟
#!/bin/sh YrqLinux联盟
if [ "$SHELL" = "/bin/bash" ]; then YrqLinux联盟
echo "your login shell is the bash (bourne again shell)" YrqLinux联盟
else YrqLinux联盟
echo "your login shell is not bash but $SHELL" YrqLinux联盟
fi YrqLinux联盟
变量$SHELL包含了登录shell的名称,我们和/bin/bash进行了比较。 YrqLinux联盟
快捷操作符 YrqLinux联盟
熟悉C语言的朋友可能会很喜欢下面的表达式: YrqLinux联盟
[ -f "/etc/shadow" ] && echo "This computer uses shadow passwors" YrqLinux联盟
这里 && 就是一个快捷操作符,如果左边的表达式为真则执行右边的语句。YrqLinux联盟
您也可以认为是逻辑运算中的与操作。上例中表示如果/etc/shadow文件存在YrqLinux联盟
则打印” This computer uses shadow passwors”。同样或操作(||)在shell编程中也是YrqLinux联盟
可用的。这里有个例子: YrqLinux联盟
#!/bin/sh YrqLinux联盟
mailfolder=/var/spool/mail/james YrqLinux联盟
[ -r "$mailfolder" ]' '{ echo "Can not read $mailfolder" ; exit 1; } YrqLinux联盟
echo "$mailfolder has mail from:" YrqLinux联盟
grep "^From " $mailfolder
该脚本首先判断mailfolder是否可读。如果可读则打印该文件中的"From" 一行。如果不可读YrqLinux联盟
则或操作生效,打印错误信息后脚本退出。这里有个问题,那就是我们必须有两个命令: YrqLinux联盟
-打印错误信息 YrqLinux联盟
-退出程序 YrqLinux联盟
我们使用花括号以匿名函数的形式将两个命令放到一起作为一个命令使用。一般函数将在下文提及。 YrqLinux联盟
不用与和或操作符,我们也可以用if表达式作任何事情,但是使用与或操作符会更便利很多。
2.case
case :表达式可以用来匹配一个给定的字符串,而不是数字。 YrqLinux联盟
case ... in YrqLinux联盟
...) do something here ;; YrqLinux联盟
esac YrqLinux联盟
让我们看一个例子。 file命令可以辨别出一个给定文件的文件类型,比如: YrqLinux联盟
file lf.gz YrqLinux联盟
这将返回: YrqLinux联盟
lf.gz: gzip compressed data, deflated, original filename, YrqLinux联盟
last modified: Mon Aug 27 23:09:18 2001, os: Unix YrqLinux联盟
我们利用这一点写了一个叫做smartzip的脚本,该脚本可以自动解压bzip2, gzip 和zip 类型的压缩文件: YrqLinux联盟
#!/bin/sh YrqLinux联盟
ftype=`file "$1"` YrqLinux联盟
case "$ftype" in YrqLinux联盟
"$1: Zip archive"*) YrqLinux联盟
unzip "$1" ;; YrqLinux联盟
"$1: gzip compressed"*) YrqLinux联盟
gunzip "$1" ;; YrqLinux联盟
"$1: bzip2 compressed"*) YrqLinux联盟
bunzip2 "$1" ;; YrqLinux联盟
*) echo "File $1 can not be uncompressed with smartzip";; YrqLinux联盟
esac YrqLinux联盟
您可能注意到我们在这里使用了一个特殊的变量$1。该变量包含了传递给该程序的第一个参数值。YrqLinux联盟
也就是说,当我们运行: YrqLinux联盟
smartzip articles.zip YrqLinux联盟
$1 就是字符串 articles.zip
3. selsectYrqLinux联盟
select 表达式是一种bash的扩展应用,尤其擅长于交互式使用。用户可以从一组不同的值中进行选择。 YrqLinux联盟
select var in ... ; do YrqLinux联盟
break YrqLinux联盟
done YrqLinux联盟
.... now $var can be used .... YrqLinux联盟
下面是一个例子: YrqLinux联盟
#!/bin/sh YrqLinux联盟
echo "What is your favourite OS?" YrqLinux联盟
select var in "Linux" "Gnu Hurd" "Free BSD" "Other"; do YrqLinux联盟
break YrqLinux联盟
done YrqLinux联盟
echo "You have selected $var" YrqLinux联盟
下面是该脚本运行的结果: YrqLinux联盟
What is your favourite OS? YrqLinux联盟
1) Linux YrqLinux联盟
2) Gnu Hurd YrqLinux联盟
3) Free BSD YrqLinux联盟
4) Other YrqLinux联盟
#? 1 YrqLinux联盟
You have selected Linux
4.loop
loop表达式: YrqLinux联盟
while ...; do YrqLinux联盟
.... YrqLinux联盟
done YrqLinux联盟
while-loop 将运行直到表达式测试为真。will run while the expression that we test for is true. YrqLinux联盟
关键字"break" 用来跳出循环。而关键字”continue”用来不执行余下的部分而直接跳到下一个循环。
for-loop表达式查看一个字符串列表 (字符串用空格分隔) 然后将其赋给一个变量: YrqLinux联盟
for var in ....; do YrqLinux联盟
.... YrqLinux联盟
done YrqLinux联盟
在下面的例子中,将分别打印ABC到屏幕上: YrqLinux联盟
#!/bin/sh YrqLinux联盟
for var in A B C ; do YrqLinux联盟
echo "var is $var" YrqLinux联盟
done YrqLinux联盟
下面是一个更为有用的脚本showrpm,其功能是打印一些RPM包的统计信息: YrqLinux联盟
#!/bin/sh YrqLinux联盟
# list a content summary of a number of RPM packages YrqLinux联盟
# USAGE: showrpm rpmfile1 rpmfile2 ... YrqLinux联盟
# EXAMPLE: showrpm /cdrom/RedHat/RPMS/*.rpm YrqLinux联盟
for rpmpackage in $*; do YrqLinux联盟
if [ -r "$rpmpackage" ];then YrqLinux联盟
echo "=============== $rpmpackage ==============" YrqLinux联盟
rpm -qi -p $rpmpackage YrqLinux联盟
else YrqLinux联盟
echo "ERROR: cannot read file $rpmpackage" YrqLinux联盟
fi YrqLinux联盟
done YrqLinux联盟
这里出现了第二个特殊的变量$*,该变量包含了所有输入的命令行参数值。YrqLinux联盟
如果您运行showrpm openssh.rpm w3m.rpm webgrep.rpm YrqLinux联盟
此时 $* 包含了 3 个字符串,即openssh.rpm, w3m.rpm and webgrep.rpm.
YrqLinux联盟
5. 引号 YrqLinux联盟
在向程序传递任何参数之前,程序会扩展通配符和变量。这里所谓扩展的意思是程序会把通配符YrqLinux联盟
(比如*)替换成合适的文件名,它变量替换成变量值。为了防 止程序作这种替换,您可以使用YrqLinux联盟
引号:让我们来看一个例子,假设在当前目录下有一些文件,两个jpg文件, mail.jpg 和tux.jpg。 YrqLinux联盟
1.2 编译SHELL脚本 YrqLinux联盟
#ch#!/bin/sh mod +x filename YrqLinux联盟
cho *.jpg ∪缓螅梢酝ü淙耄?./filename 来执行您的脚本。 YrqLinux联盟
这将打印出"mail.jpg tux.jpg"的结果。 YrqLinux联盟
引号 (单引号和双引号) 将防止这种通配符扩展: YrqLinux联盟
#!/bin/sh YrqLinux联盟
echo "*.jpg" YrqLinux联盟
echo '*.jpg' YrqLinux联盟
这将打印"*.jpg" 两次。 YrqLinux联盟
单引号更严格一些。它可以防止任何变量扩展。双引号可以防止通配符扩展但允许变量扩展。 YrqLinux联盟
#!/bin/sh YrqLinux联盟
echo $SHELL YrqLinux联盟
echo "$SHELL" YrqLinux联盟
echo '$SHELL' YrqLinux联盟
运行结果为: YrqLinux联盟
/bin/bash YrqLinux联盟
/bin/bash YrqLinux联盟
$SHELL YrqLinux联盟
最后,还有一种防止这种扩展的方法,那就是使用转义字符——反斜杆: YrqLinux联盟
echo *.jpg YrqLinux联盟
echo $SHELL YrqLinux联盟
这将输出: YrqLinux联盟
*.jpg YrqLinux联盟
$SHELL
Linux联盟收集整理 ,转贴请标明原始链接,如有任何疑问欢迎来本站Linux论坛讨论