打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Bourne Shell及shell编程 2
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结构类似,但结构更清淅,其执行结果完全相同.
 
/********抱歉为了格式不乱我用代码模式粘贴全文*********/
------------------------------------------------------------------------------
源码:
 
 
Bourne Shell及Shell编程(2)
 h.交互式从键盘读入数据
 使用read语句其格式如下
 
 read var1 var2 ... varn
 
 read将不作变量替换但会删除多余的空格直到遇到第一个换
行符回车
 并将输入值依次赋值给相应的变量
 
 例
 $ read var1 var2 var3
 Hello my friends

 $ echo $var1 $var2 $var3
 Hello my friends
 $ echo $var1
 Hello
 $ read var1 var2 var3
 Hello my dear friends
 $ echo $var3
 dear friends <-输入多余变量时输入值余下的内容赋给最后一
个变量
 $ read var1 var2 var3
 Hello friends
 $ echo $var3
 <- var3为空
 $
 
 在shell script中可使用read语句进行交互操作
 
 ...
 #echo -n message 输出结果后不换行
 echo -n "Do you want to continue: Y or N"
 read ANSWER
 
 if [ $ANSWER=N -o $ANSWER=n ]
 then
 exit
 fi
 
 i. case结构结构较elif-then结构更清楚
 
 比较if-then语句
 
 if [ variable1 = value1 ]
 then
 command
 command
 elif [ variable1 = value2 ]
 then
 command
 command
 elif [ variable1 = value3 ]
 then
 command
 command
 fi

 
 相应的case结构
 
 case value in
 pattern1)
 command
 command;;
 pattern2)
 command
 command;;
 ...
 patternn)
 command;
 esac
 
 * case语句只执行第一个匹配模式
 
 例使用case语句建立一个菜单选择shell script
 
 #Display a menu
 echo _
 echo "1 Restore"
 echo "2 Backup"
 echo "3 Unload"
 echo
 
 #Read and excute the user's selection
 echo -n "Enter Choice:"
 read CHOICE
 
 case "$CHOICE" in
 1) echo "Restore";;
 2) echo "Backup";;
 3) echo "Unload";;
 *) echo "Sorry $CHOICE is not a valid choice
 exit 1
 esac
 
 在上例中*指默认匹配动作此外case模式中也可以使用逻辑
操作如下所示
 
 
 pattern1 | pattern2 ) command
 command ;;

 
 这样可以将上面示例程序中允许用户输入数字或每一个大写字母

 
 case "$CHOICE" in
 1|R) echo "Restore";;
 2|B) echo "Backup";;
 3|U) echo "Unload";;
 *) echo "Sorry $CHOICE is not a valid choice

 exit 1
 esac

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
Unix系列shell程序编写(中)
一些常用linux命令列表
shell程序编写从入门到精通(中)
Sun Solaris指令大全
Solaris 命令 小结
系统管理中 bash shell 脚本常用方法总结
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服