运维shell全部语法进阶
一、if语句的使用
1)语法规则
|
1
2
3
4
5
6
7
8
9
|
if [条件] then 指令fi或if [条件];then 指令fi提示:分号相当于命令换行,上面两种语法等同<br>特殊写法;if[ -f"$file1" ];then echo 1;fi 相当于[ -f"$file1" ] && echo 1 |
2)多分支结构语法
|
1
2
3
4
5
6
7
8
9
10
|
多分支结构;语法if 条件 then 指令集elif 条件 #多个 then 指令集else 指令集fi |
3)比较大小的案例
案例一,交互式的
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#!/bin/shread -p "pls input two num:" a bif [ $a -lt $b ];then echo "yes,$a less than $b" exitfiif [ $a -eq $b ];then echo "yes,$a eaual than $b" exitfiif [ $a -gt $b ];then echo "yes,$a greater than $b" exitfi |
案例二,命令行输入比较大小
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
[root@tomcat day1]# cat ifif.sh#!/bin/sha=$1b=$2[ $# -ne 2 ] && { echo "USAGE:$0 NUM1 NUM2" exit 1}expr $a + 0 &>/dev/nullRETVAL1=$?expr $b + 0 &>/dev/nullRETVAL2=$?test $RETVAL1 -eq 0 -a $RETVAL2 -eq 0 ||{ echo "Pla input two intnum again" exit 2}if [ $a -lt $b ] then echo "$a < $b"elif [ $a -eq $b ] then echo "$a = $b"else echo "$a > $b"fiexit 0 |
案例三,比较大小经典版
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
[root@oldboy66 day2]# cat if_else.sh#!/bin/shif [ $1 -eq $2 ] then echo "$1=$2" exitelif [ $1 -gt $2 ] then echo "$1>$2" exitelse echo "$1<$2" exitfi |
4)判定在特定目录下创建文件的案例
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
[root@oldboy66 day2]# cat if4.sh#!/bin/shpath=/server/scriptsfile=if3.shif [ ! -d $path ];then mkdir -p $path echo "$path is not exist,already created it."fiif [ ! -f $path/$file ];then touch $path/$file echo "$path/$file is not exist,already created it." exitfiecho "ls -l $path/$file"ls -l $path/$file |
5)查看内存,测试邮件报警
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
1)安装sendmail邮件工具yum install sendmail -y/etc/init.d/sendmail start2)获取内存大小[root@oldboy66 day2]# free -m total used free shared buffers cachedMem: 980 906 73 0 135 489-/+ buffers/cache: 282 698Swap: 511 0 511[root@oldboy66 day2]# free -m|grep buffers/-/+ buffers/cache: 281 698[root@oldboy66 day2]# free -m|grep buffers/|awk '{print $NF}'6983)写入脚本[root@oldboy66 day3]# cat check_mem.sh#!/bin/shused_men=`free -m|awk 'NR==3 {print $NF}'`if [ $used_men -lt 800 ];then echo "men is not enough,$used_men" echo "men is not enough.$used_men."|mail -s "men warning $(date +%F)" 1111111111@qq.comfi4)qq邮箱设置打开qq邮箱===》“设置” ====》“反垃圾”=====》“设置邮件地址白名单”=====》添加“root@tomcat.localdomain”5)执行脚本,收到邮件6)写入定时任务[root@oldboy66 ~]# crontab -e###发邮件mail,mutt。Sendmail服务要开启,定时任务报警*/3 * * * * /bin/sh /server/scripts/day3/check_mem.sh &>/dev/null |
二、检测mysql服务是否启动,如果没启动,就去启动
1)检测思路
|
1
2
3
4
5
6
7
8
9
10
|
netstat -lntup|grep 3306 看端口ps -ef|grep mysql 看进程mysql -u root -p123456 -S /data/3307/mysql.sock -e "select version();" (多实例)登录进去看版本取返回值 +-----------+ | version() | +-----------+ | 5.5.32 | +-----------+mysql -u root -poldboy -S /data/3307/mysql.sock -e "select version();" &>/dev/nullecho $? |
2)检测启动脚本
方法一;根据端口
|
1
2
3
4
5
6
7
8
9
10
|
mysql单实例检测端口[root@tomcat ]# cat port.sh#!/bin/shport=`netstat -lntup|grep 3306|wc -l`if [ $port -ne 1 ] then /etc/init.d/mysqld start else echo "MySQL is running"fi |
方法二;根据进程
|
1
2
3
4
5
6
7
8
9
10
11
|
mysql检测进程[root@tomcat ]# cat process.sh#!/bin/shprocess=`ps -ef|grep mysql|grep -v grep|wc -l`if [ $process -ne 2 ] then /etc/init.d/mysqld start else echo "MySQL is running"fi### sh -x process.sh #注意事项 调试。使用进程脚本不要用到mysql的名字 |
三、检查web服务是否启动
1)简单的检查web是否启动
|
1
2
3
4
5
6
7
8
9
|
[root@linux day3]# cat check_web.sh#!/bin/shhttp_code=`curl -I -s -w "%{http_code}" -o /dev/null 192.168.1.50:50080`if [ $http_code -ne 200 ] then echo "web is error" else echo "web is ok"fi |
2)利用系统函数,实现脚本启动的特殊颜色效果,开发web服务的启动脚本
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
[root@linux day4]# cat start_nginx.sh#!/bin/sh. /etc/init.d/functionsif [ $# -ne 1 ] then echo "USAGE $0 {start|stop|restart}" exit 1fiif [ "$1" == "start" ] then action "start nginx" /bin/trueelif [ "$1" == "stop" ] then action "stop nginx" /bin/trueelif [ "$1" == "restart" ] then action "restart nginx" /bin/trueelse echo "USAGE $0 {start|stop|restart}" exit 1fi |
3)增加函数功能,实现上面的例子
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
[root@linux day4]# cat start_nginx02.sh#!/bin/sh. /etc/init.d/functionsstart_nginx=/application/nginx/sbin/nginxUSAGE(){ echo "USAGE $0 {start|stop|restart}" exit 1}if [ $# -ne 1 ] then USAGEfiif [ "$1" == "start" ] then $start_nginx action "start nginx" /bin/trueelif [ "$1" == "stop" ] then killall nginx action "stop nginx" /bin/trueelif [ "$1" == "restart" ] then pkill nginx sleep 2 $start_nginx action "restart nginx" /bin/trueelse USAGE exit 1fi |
四、shell函数
1)函数语法
|
1
2
3
4
5
6
7
8
9
10
11
12
|
shell函数语法函数名(){ 指令 return n}或function 函数名(){ 指令 return n} |
2)函数说明
|
1
2
3
4
5
6
7
8
|
【函数带参数的说明】1:函数体中位置参数($1、$2、$3、$4、$5、$#、$*、$?以及$@)都可以是函数的参数2:父脚本的参数则临时地被函数参数所掩盖或隐藏3:$0比较特殊,它仍然是父脚本的名称4:当函数完成时,原来的命令行参数会恢复5:在shell函数里面,return命令的功能的工作方式与exit相同,用于跳出函数6:在shell函数体里使用exit会终止整个shell脚本7:return语句会返回一个退出值给调用的程序 |
3)函数调用例子
|
1
2
3
4
5
6
7
8
|
[root@linux day4]# cat fun01.sh#!/bin/sholdboy01(){ echo "I am caojin linux"}oldboy01[root@linux day4]# sh fun01.shI am caojin linux |
4)函数传参,判断web服务是否正常
|
1
2
3
4
5
6
7
8
9
10
11
|
[root@linux day4]# cat check_web_by_fun.sh#!/bin/shfunction Check_Url(){ curl -I -s $1 |head -1 && return 0||return 1}Check_Url $1[root@linux day4]# sh check_web_by_fun.sh 192.168.1.50:50080HTTP/1.1 200 OK[root@linux day4]# sh check_web_by_fun.sh baidu.comHTTP/1.1 200 OK |
五、开发mysql·的启动脚本
1)简单版
View Code2)优化版,去掉mysql的启动输出
View Code3)添加到开机启动
|
1
2
3
4
5
6
7
8
|
1)测试OK2)cp start_db.sh /etc/init.d/mysqld3)chmod +x /etc/init.d/mysqld4)chkconfig --list mysqld5)chkconfig --add mysqld6)chkconfig mysqld on7)chkconfig --list mysqld8)ll /etc/rc.d/rc3.d/|grep mysqld# chkconfig: 2345 21 60 #2345 启动级别, #21 开机启动顺序, # 60 关机顺序 |
六、输出颜色方法
1)echo 输出字符串显示不同颜色范例
|
1
2
3
4
5
6
7
8
|
echo -e "\033[30m 黑色字caojin tarinning \033[0m"echo -e "\033[31m 红色字caojin tarinning \033[0m"echo -e "\033[32m 绿色字caojin tarinning \033[0m"echo -e "\033[33m 黄色字caojin tarinning \033[0m"echo -e "\033[34m 蓝色字caojin tarinning \033[0m"echo -e "\033[35m 紫字caojin tarinning \033[0m"echo -e "\033[36m 天蓝字caojin tarinning \033[0m"echo -e "\033[37m 白色字caojin tarinning \033[0m" |
2)字背景颜色范围:40------47
|
1
2
3
4
5
6
7
8
|
echo -e "40;37m 黑底白字 whlcome to China\033[0m"echo -e "41;37m 黑底白字 whlcome to China\033[0m"echo -e "42;37m 黑底白字 whlcome to China\033[0m"echo -e "43;37m 黑底白字 whlcome to China\033[0m"echo -e "44;37m 黑底白字 whlcome to China\033[0m"echo -e "45;37m 黑底白字 whlcome to China\033[0m"echo -e "46;37m 黑底白字 whlcome to China\033[0m"echo -e "47;30m 黑底白字 whlcome to China\033[0m" |
3)简单颜色脚本
View Code七、case结构条件句
1)基本语法
|
1
2
3
4
5
6
7
|
case "字符串变量" in 值1)指令1...;; 值2)指令2...;; *)指令...esac |
2)case创建水果菜单,增加特殊颜色
View Code3)利用传参的形式给对象增加颜色
View Code4)案例开发类似于rsync的启动脚本
View Code注意:此脚本并不完善,可以加载函数,添加颜色,让其开机自启动(chkconfig)
八、while循环,以及until循环
1)while语法
|
1
2
3
4
|
while 条件句 do 指令...done |
2)until语法
|
1
2
3
4
|
until 条件 do 指令....done |
3)while循环,守护进程举例
提示:while true表示永远为真,因此会一直运行,像死循环一样,但是我们称呼为守护进程
|
1
2
3
4
5
6
7
|
[root@linux day5]# cat while.sh#!/bin/shwhile truedo uptime sleep 2done |
脚本在后台执行
|
1
2
3
4
5
6
7
|
脚本在后台执行知识扩展:功能 用途sh while.sh & 把脚本while.sh放到后台执行ctrl+c 停止执行当前脚本或任务ctrl+z 暂停执行当前脚本或任务bg 把当前脚本或任务放到后台执行 backgroundfg 当前脚本或任务拿到前台执行,如果 |
如果执行的脚本忘记在后台执行
|
1
2
3
4
5
6
7
|
[root@oldboy66 day5]# sh while.sh^Z[1]+ Stopped sh while.sh[root@oldboy66 day5]# bg[1]+ sh while.sh &[root@oldboy66 day5]# |
4)while计算1加到100的和
方法一
|
1
2
3
4
5
6
7
8
9
10
11
12
|
[root@linux day5]# cat while_sum.sh#!/bin/shi=1sum=0while [ $i -le 100 ]do let sum=sum+i let i=i+1doneecho $sum[root@oldboy66 day5]# sh while_sum.sh5050 |
方法二
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[root@linux day5]# sh while_sum.sh5050或[root@oldboy66 day5]# cat while_sum2.sh#!/bin/shi=1sum=0while ((i < 101))do ((sum=sum+i)) ((i++))doneecho $sum |
5)计算Apache一天的日志access_2016-12-8.log中所有行的日志各元素的访问字节数的总和。给出实现程序。用while循环实现
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[root@linux day5]# cat log.sh#!/bin/shsum=0i=0while read linedo i=$(echo $line|awk '{print $10}') if expr $i + 0 &>/dev/null then ((sum=sum+i)) fidone <access_2015_12_8.logecho $sum |
6)while循环做抓阄小游戏
要求:
[ 1 ]每个人都输入名字,然后随机产生不同的数字(1--99)
[ 2 ]第一个输入名字后,屏幕输出信息,并将名字和数字记录到文件里,程序不能退出,继续等待别的学生输入。
[ 3 ]输出的名字与对应的数字的最大则是被抓到的人
View Code七、rsync 数据同步
范例:每10秒钟做一次rsync binlog推送,通过守护进程方式,写完脚本后台执行。当配好rsync服务时,可以直接用的脚本
|
1
2
3
4
5
6
7
|
[root@linux day5]# cat rsync_binlog.sh#!/bin/shwhile truedo rsync -az /data/3306/mysql-bin* rsync_backup@192.168.1.49::backup --password-file=/etc/rsync.password & sleep 10done |
八、for循环
1)for循环简单例子
|
1
2
3
4
5
6
|
[root@linux day6]# cat for.sh#!/bin/shfor n in 5 4 3 2 1do echo $ndone |
2)开发脚本实现仅设置sshd rsyslog crond network sysstat开机自启动
|
1
2
3
4
5
6
7
8
9
10
|
[root@linux day6]# cat auto_start.sh#!/bin/shfor name in `chkconfig --list|grep 3:on|awk '{print $1}'`do chkconfig $name offdonefor name in rsyslog network crond sshd systtatdo chkconfig $name ondone |
3)for循环在/oldboy目录下批量创建10个文件,名称依次为:oldboy-1.html.....
|
1
2
3
4
5
6
7
|
[root@linux day6]# cat for_mkdir.sh#!/bin/sh[ ! -d /oldboy ] && mkdir -p /oldboyfor i in `seq 10`do touch /oldboy/oldboy-${i}.htmldone |
4)用for循环实现将以上文件名中的oldboy全部改为Linux,并且扩展名改为大写。要求for循环的循环体不能出现oldboy字符串
View Code批量改名案例:http://oldboy.blog.51cto.com/2561410/711342
5)批量创建10个用户并设置密码
View Code九、随机数
1)获取随机数的7个方法
|
1
2
3
4
5
6
7
|
[root@linux day6]# echo $RANDOM[root@linux day6]# openssl rand -base64 8[root@linux day6]# date +%s%N[root@linux day6]# head /dev/urandom|cksum[root@linux day6]# cat /proc/sys/kernel/random/uuid[root@linux day6]# yum install expect -y[root@linux day6]# mkpasswd -l 8 |
2)生产随机数
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
[root@oldboy66 day6]# echo $RANDOM17123[root@oldboy66 day6]# echo $RANDOM23696[root@oldboy66 day6]# echo $((RANDOM+10000000))10028068[root@oldboy66 day6]# echo $((RANDOM+10000000))10016282[root@oldboy66 day6]# echo $RANDOM|md5sum|cut -c 1-8100764b3[root@oldboy66 day6]# echo $RANDOM|md5sum|cut -c 1-83447b18d下面是高级随机[root@oldboy66 day6]# echo "`date`$RANDOM"|md5sum|cut -c 1-8c78d73a8[root@oldboy66 day6]# echo "`date`$RANDOM"|md5sum|cut -c 1-882d4b31e |

浙公网安备 33010602011771号