1、Linux shell,Page 2/28,脚本概念,无需编译,解释执行 文本文件形式存在 强大的正则表达式操作 运行速度慢 数据类型支持少 用于系统管理和文件操作,Page 3/28,脚本运行,编写脚本程序 改变脚本程序文件的权限为可执行 运行脚本程序 # ./脚本程序 # bash 脚本程序 # . ./脚本程序 # exec 脚本程序,Page 4/28,基本脚本编程,变量 条件测试 流程控制,Page 5/28,环境变量,使用set命令查看环境变量 set 常用环境变量 USER UID SHELL HOME PWD PATH PS1 PS2 环境变量配置文件 /etc/bashrc
2、/etc/profile /.bash_profile /.bashrc,Page 6/28,位置变量,$ ./exam01 one two tree four five six,Page 7/28,预定义变量,Page 8/28,用户自定义变量,自定义变量的设置 $ DAY=sunday 自定义变量的查看与引用 $ echo $DAY 自定义变量的输出 $ export DAY 自定义变量的清除 $ unset DAY,Page 9/28,脚本,第一行必须指出用哪个程序来执行脚本,内核根据它来确定用哪个程序来翻译脚本中的行如: #!/bin/bash 注释,以“”开头的文字描述 可执行语句与
3、bash shell 结构 例: vi firstscript #!/bin/bash #first script echo “Hello world !”,Page 10/28,条件测试,使用test命令 语法:使用括号 语法: 条件表达式的值为真返回零,为假时返回非零值 man test(显示测试表达示的写法),test 条件表达式, 条件表达式 ,Page 11/28,条件测试篇,文件状态测试,示例: -s haison.c 0表示成功,其他为失败,下同。,逻辑操作符-a 逻辑与(and),操作符两边均为真,结果为真,否则为假。-o 逻辑或(or),操作符两边一边为真,结果为真,否则为假
4、。! 逻辑否,条件为假,结果为真,反之。,示例: -r haison.c a w hai.c -r baison.c & -w bai.c ,Page 12/28,条件测试篇,字符串测试,示例: -z $SHELL ,数值测试,示例: “10” -eq “12” ,Page 13/28,控制结构篇,Page 14/28,流程控制,if then else语句 语法if 条件语句1 then命令1elif 条件语句2 then命令2else命令3fi,(条件1为真),(条件1为假,条件2为真),(条件2为假),Page 15/28,流程控制,#!/bin/bash #iftest1 echo “
5、input one number:“ read a echo “input the other number:“ read b if “$a“ -lt “$b“ thenecho “$a is less than $b“ elseecho “$a is greater than $b“ fi,Page 16/28,流程控制,#!/bin/bash #iftest2 echo “input the fist number:“ read a echo “input second number:“ read b echo “input third number:“ read c if $a -gt
6、$b & $b -gt $c thenecho “$a is max“elif $b -gt $a & $a -gt $c thenecho “ $b is max“ elseecho “$c is max“ fi,Page 17/28,流程控制(Cont.),case语句 语法case 值 in模式1)命令1;模式2)命令2;模式3)命令3;esac,Page 18/28,流程控制(Cont.),#!/bin/bash #case select case $1 instart)echo “command is starting ok“;stop)echo “command is stopin
7、g ok“;restart)echo “command is stoping ok“echo “command is starting ok“;*)echo “Usage $0 stop|start|restart“; esac,Page 19/28,流程控制(Cont.),for循环 语法,for 变量名 in 列表do命令done,Page 20/28,流程控制(Cont.),#/bin/bash for i in cat file doecho “$i” done,Page 21/28,流程控制(Cont.),#/bin/bash account =cut -d “:” -f1 /etc
8、/passwd | sort echo “The following is your linuxs account” for i in $account doecho “$i” done,Page 22/28,流程控制(Cont.),while循环 语法,while 条件do命令done,Page 23/28,函数篇,格式 函数名 () 命令 所有函数在使用前必须定义,这意味着必 须将函数放在脚本开始部分,直至shell解释 器首次发现它,才可以使用。调用函数仅使用其函数名即可,要传给函 数的变量跟在函数后面。函数里面定义的变量以下划线(_)开始。函数可以放在同一个文件中作为一段代 码,也可以
9、放在只包含函数的单独文件中,文 件也必须以#!/bin/bash开头。,示例1 #!/bin/bash #funTest #to test the function DATE=date Hello() echo “Hello,today is $DATE” Hello,示例2 #!/bin/bash #funTest #to test the function. ./Hello Hello,Page 24/28,习题,1、批量创建用户: #!/bin/bash i=$2 while $i -le $1 do adduser $3$i; echo “$3$i:$3$i“ pass; chpass
10、wd pass; let i=i+1; done,Page 25/28,习题,2、批量删除用户: #!/bin/bash #batch del user i=$2 While $i le “$1” do userdel r user$3$i let i=i+1 done,Page 26/28,习题,3、批量删除用户(用户创建的用户): #!/bin/bash #batch del user ls /home txt for i in cat txt do userdel r $i done,Page 27/28,习题,4、备份(完全备份加增量备份)(保留时间为一个月): #!/bin/bash
11、 #backup if date +%d = “01” #每月1日完全备份 then rm f /mnt/sda1/backup/* rm f /mnt/sda1/backup/cl/* tar czf /mnt/sda1/backup/date +%Y-%m-%d.tar /var/www/html elif date +%A = “Friday” #每周五增量备份 then find /var/www/html -mtime -7 -print include.txt tar -cz -T include.txt f /mnt/sda1/backup/cl/date +%Y-%m-%d.tar.gz fi,