[转载]Bourne Shell及shell编程 二
时间:2005-11-26 来源:ghostzhu
版权声明:
本文内容为大连理工大学LINUX选修课讲义,欢迎大家转载,但禁止使用本材料进行
任何商业性或赢利性活动。转载时请保留本版权声明。
作者:何斌武,[email protected],大连理工大学网络中心,April 1999.
[请点击阅读全文]
——by ghostzhu
(2)位置变量(Shell参数)
在shell script中位置参数可用$1..$9表示,$0表示内容通常为当前执行程序的文件名
。
.防止变量值被替换 readonly variable
.使用export命令输出变量,使得变量对子shell可用,当shell执行一下程序时,shell
将为其设置一个新的环境让其执行,这称之了subshell. 在Bourne Shell中变量通常
被认为是本地变量,也就是说在对其赋值之外的shell环境之外是不认识此变量的。使
用export对subshell可用。
例:对变量PS1的export操作,shell的提示符将发生变化。
$ PS1=`hostname`$
peony$sh
$ echo $PS1
$ <-输出结果
$ exit
peony$export PS1
peony$sh
peony$ echo $PS1
peony$ <-输出结果
peony$
3.Shell Script编程
目的:使用UNIX所提供的最常用工具来完成所需复杂任务的强大功能。
(1)最简单的Shell 编程
$ls -R / |grep myname |more
每天数据的备份:
$ cd /usr/yourname; ls * |cpio -o > /dev/rmt/0h
书写程序的目的是一次编程,多次使用(执行)!
$ cat > backup.sh
cd /home/hbwork
ls * | cpio -o > /dev/rmt/0h
^D
执行:
$ sh backup.sh
或:
$ chmod +x backup.sh
$ ./backup.sh
技巧:在shell script中加入必要的注释,以便以后阅读及维护。
(2)shell是一个(编程)语言
同传统的编程语言一样,shell提供了很多特性,这些特性可以使你的shell script
编程更为有用,如:数据变量、参数传递、判断、流程控制、数据输入和输出,子
程序及以中断处理等。
. 在shell编程中使用数据变量可以将程序变量更为通用,如在上面backup.sh中:
cd $WORKDIR
ls * | cpio -o > /dev/rmt/0h
. Shell编程中的注释以#开头
. 对shell变量进行数字运算,使用expr命令
expr integer operator integer
其中operator为+ - * / %, 但对*的使用要用转义符,如:
$expr 4 * 5
20
$int=`expr 5 + 7`
$echo $int
12
(3)Shell编程的参数传递, 可通过命令行参数以及交互式输入变量(read)
restoreall.sh 对backup.sh程序的备份磁带进行恢复
$cat > restoreall.sh
cd $WORKDIR
cpio -i < /dev/rmt/0h
^D
restore1.sh:只能恢复一个文件
#restore1 --program to restore a single file
cd $WORKDIR
cpio -i $i < /dev/rmt/0h
$restore1 file1
恢复多个文件restoreany :
#restoreany --program to restore a single file
cd $WORKDIR
cpio -i $* < /dev/rmt/0h
$ restoreany file1 file2 file3
(4)条件判断
. if-then语句,格式如下:
if command_1
then
command_2
command_3
fi
command_4
在if-then语句中使用了命令返回码$?,即当command_1执行成功时才执行command_2和
command_3,而command_4总是执行.
示例程序unload: 在备份成功时删除原始文件,带有错误检查
cd $1
#备份时未考虑不成功的情况!
ls -a | cpio -o > /dev/rmt/0h
rm -rf *
改进如下:
#当使用了管道命令时,管理命令的返回代码为最后一个命令的返回代码
if ls -a | cpio -o > /dev/rmt/0h
then
rm -rf *
fi
. if-then-else语句
if command_1
then
command_2
else
command_3
fi
技巧: 由于shell对命令中的多余的空格不作任何处理,一个好的程序员会用这一
特
性
对自己的程序采用统一的缩进格式,以增强自己程序的可读性.
. 使用test命令进行进行条件测试
格式: test conditions
test在以下四种情况下使用: a. 字符比较 b.两个整数值的比较
c. 文件操作,如文件是否存在及文件的状态等
d. 逻辑操作,可以进行and/or,与其他条件联合使用
a. 测试字符数据: shell变量通常民政部下均作为字符变量
str1 = str2 二者相长,相同
str1 != str2 不同
-n string string不为空(长度不为零)
-z string string为空
string string不为空
例:
$ str1=abcd #在含有空格时必须用引号括起来
$ test $str1=abcd
$ echo $?
0
$ str1="abcd "
$ test $str1=abcd
$ echo $?
1
Note: 在test处理含有空格的变量时最好用引号将变量括起来,否则会出现错误的
结果,
因为shell在处理命令行时将会去掉多余的空格,而用引号括起来则可以防
止
shell去掉这些空格.
例:
$ str1=" "
$ test $str1
$ echo $?
1
$ test "$str1"
$ echo $?
0
$ test -n $str1
test: argument expected
$ test -n "$str1"
$ echo $?
0
$
b. 整数测试: test与expr相同,可以将字符型变量转换为整数进行操作,expr进行
整数的算术运算,而test则进行逻辑运算.
表达式 说明
---------------------------------------
int1 -eq int2 相等?
int1 -ne int2 不等?
int1 -gt int2 int1 > int2 ?
int1 -ge int2 int1 >= int2 ?
int1 -lt int2 int1 < int2 ?
int1 -le int2 int1 <= int2 ?
例:
$ int1=1234
$ int2=01234
$ test $int1 -eq $int2
$ echo $?
0
c. 文件测试:检查文件状态如存在及读写权限等
-r filename 用户对文件filename有读权限?
-w filename 用户对文件filename有写权限?
-x filename 用户对文件filename有可执行权限?
-f filename 文件filename为普通文件?
-d filename 文件filename为目录?
-c filename 文件filename为字符设备文件?
-b filename 文件filename为块设备文件?
-s filename 文件filename大小不为零?
-t fnumb 与文件描述符fnumb(默认值为1)相关的设备是一个终端设备?
d. 测试条件之否定,使用!
例:
$ cat /dev/null > empty
$ test -r empty
$ echo $?
0
$ test -s empty
1
$ test ! -s empty
$ echo $?
0
e. 测试条件之逻辑运算
-a And
-o Or
例: $ test -r empty -a -s empty
$ echo $?
1
f. 进行test测试的标准方法
因为test命令在 shell编程中占有很重要的地位,为了使shell能同其他编程语言
一样
便于阅读和组织, Bourne Shell在使用test测试时使用了另一种方法:用方括号
将
整个
test测试括起来:
$ int1=4
$ [ $int1 -gt 2 ]
$ echo $?
0
例: 重写unload程序,使用test测试
#!/bin/sh
#unload - program to backup and remove files
#syntax: unload directory
#check arguments
if [ $# -ne 1 ]
then
echo "usage: $0 directory"
exit 1
fi
#check for valid directory name
if [ ! -d "$1" ]
then
echo "$1 is not a directory"
exit 2
fi
cd $1
ls -a | cpio -o > /dev/rmt/0h
if [ $? -eq 0 ]
then
rm -rf *
else
echo "A problem has occured in creating backup"
echo "The directory will not be ereased"
echo "Please check the backup device"
exit 3
fi
# end of unload
在如上示例中出现了exit, exit有两个作用:一是停止程序中其他命令的执行,二
是
设置程序的退出状态
g. if嵌套及elif结构
if command
then
command
else
if command
then
command
else
if command
then
command
fi
fi
fi
改进:使用elif结构
if command
then
command
elif command
then
command
elif command
then
command
fi
elif结构同if结构类似,但结构更清淅,其执行结果完全相同.