shell编程知识

shell编程
shell是一种脚本语言
可以使用逻辑判断、循环等语法
可以自定义函数
shell是系统命令的集合
shell脚本可以实现自动化运维,能大大增加我们的运维效率

开头需要加#!/bin/bash
以#开头的行作为解释说明
脚本的名字以.sh结尾,用于区分这是一个shell脚本
执行方法有两种
chmod +x 1.sh; ./1.sh
bash 1.sh
查看脚本执行过程 bash -x 1.sh
查看脚本是否语法错误 bash -n 1.sh

date时间命令
date  +%Y-%m-%d=date +%F 年月日
date +%y-%m-%d 年月日
date  +%H:%M:%S = date +%T 时间
date +%s  时间戳
date -d @1504620492
date -d "+1day" 一天后
date -d "-1 day" 一天前
date -d "-1 month" 一月前
date -d "-1 min" 一分钟前
date +%w 周几
date +%W 今年的第几周

shell脚本中的变量
当脚本中使用某个字符串较频繁并且字符串长度很长时就应该使用变量代替
使用条件语句时,常使用变量    if [ $a -gt 1 ]; then ... ; fi
引用某个命令的结果时,用变量替代   n=wc -l 1.txt
写和用户交互的脚本时,变量也是必不可少的  read -p "Input a number: " n; echo $n   如果没写这个n,可以直接使用$REPLY
内置变量 $0, $1, $2…    $0表示脚本本身,$1 第一个参数,$2 第二个 ....       $#表示参数个数
数学运算a=1;b=2; c=$(($a+$b))或者$[$a+$b]

shell脚本中的逻辑判断

格式1:if 条件 ; then 语句; fi a=5 if [ $a -gt 3 ] ;then echo ok ; fi
格式2:if 条件; then 语句; else 语句; fi a=5 if [ $a -gt 3 ] ; then echo ok ; else not ok ; fi
格式3:if …; then … ;elif …; then …; else …; fi a=5 if [ $a -gt 1] ; then echo ok ;elif [ $a -lt 6 ] ; else nook ; fi
逻辑判断表达式:
if [ $a -gt $b ];
if [ $a -lt 5 ];
if [ $b -eq 10 ]等
-gt (>); 大于
-lt(<); 小于
-ge(>=); 大于等于
-le(<=); 小于等于
-eq(==); 等于
-ne(!=) 不等于
注意到处都是空格
可以使用 && || 结合多个条件
if [ $a -gt 5 ] && [ $a -lt 10 ]; then 且
if [ $b -gt 5 ] || [ $b -lt 3 ]; then 或

文件目录属性判断
[ -f file ]判断是否是普通文件,且存在 f="/tmp/ceshi" if [ -f $f ] ; then echo $f exist ; else touch $f ; fi
[ -d file ] 判断是否是目录,且存在 f="/tmp/ceshi" if [ -d $f ] ; then echo $f exist ; else touch $f ; fi
[ -e file ] 判断文件或目录是否存在 f="/tmp/ceshi" if [ -e $f ] ; then echo $f exist ; else touch $f ; fi
[ -r file ] 判断文件是否可读 f=''/tmp/ceshi'' if [ -r $f ] ; then echo $f is read; fi
[ -w file ] 判断文件是否可写 f=''/tmp/ceshi'' if [ -w $f ] ; then echo $f is write; fi
[ -x file ] 判断文件是否可执行 f=''/tmp/ceshi'' if [ -x $f ] ; then echo $f is exe fi

[ -f $f ] && rm -rf $f f=''/tmp/ceshi'' if [ -f $f ] ; then rm -rf $f ; fi
[ ! -f $f ] || touch $f f=''/tmp/ceshi'' if [ ! -f $f ] ; then touch $f ;fi

if判断的一些特殊用法
if [ -z "$a" ]   这个表示当变量a的值为空时会怎么样
if [ -n "$a" ] 表示当变量a的值不为空
if grep -q '123' 1.txt; then   表示如果1.txt中含有'123'的行时会怎么样
if [ ! -e file ]; then 表示文件不存在时会怎么样
if (($a<1)); then … 等同于 if [ $a -lt 1 ]; then…
[ ] 中不能使用<,>,==,!=,>=,<=这样的符号

shell中case判断
格式case变量名 in
value1)
command
;;
value2)
command
;;
*)
command
;;
在case中可以在条件中使用|,表示或的意思,比如 2|3) command ;;
read -p ""

shell脚本案例
#!/bin/bash
read -p "Please input a number: " n
if [ -z "$n" ]
then
echo "Please input a number."
exit 1
fi

n1=echo $n|sed 's/[0-9]//g' //排查非数字情况
if [ -n "$n1" ]
then
echo "Please input a number."
exit 1
fi

if [ $n -lt 60 ] && [ $n -ge 0 ]
then
tag=1
elif [ $n -ge 60 ] && [ $n -lt 80 ]
then
tag=2
elif [ $n -ge 80 ] && [ $n -lt 90 ]
then
tag=3
elif [ $n -ge 90 ] && [ $n -le 100 ]
then
tag=4
else
tag=0
fi
case $tag in
1)
echo "not ok"
;;
2)
echo "ok"
;;
3)
echo "ook"
;;
4)
echo "oook"
;;
*)
echo "The number range is 0-100."
;;
esac

for循环
语法 for 变量名 in 条件;do ………… ;done

经典案例1
#!/bin/bash
sum=0
for i in seq 1 100
do
sum=$[$sum+$i]
echo $i
done

echo $sum

文件列表循环
#!/bin/bash
cd /etc/
for a in ls /etc/
do
    if [ -d $a ]
    then
       ls -d $a
    fi
done

while循环
语法 while 条件;do .....; done

#!/bin/bash
while :
do
    load=w|head -1|awk -F 'load average: ' '{print $2}'|cut -d. -f1
    if [ $load -gt 10 ]
    then
        top|mail -s "load is high: $load" asldkfls@11.com
    fi
    sleep 30
done

#!/bin/bash
while :
do
read -p "Please input a number: " n
if [ -z "$n" ]
then
echo "you need input sth."
continue //继续开始的循环,不往下就行
fi
n1=echo $n|sed 's/[0-9]//g'
if [ -n "$n1" ]
then
echo "you just only input numbers."
continue // //继续开始的循环,不往下就行
fi
break //调出while循环
done
echo $n

break调出循环
#!/bin/bash
for i in seq 1 5
do
echo $i
if [ $i -eq 3 ]
then
break //调出for循环
fi
echo $i
done
echo 111

continue结束本次循环
结束本次的循环,忽略后面

exit直接退出脚本 直接退出脚本

shell脚本中的函数
函数就是把一段代码整理到了一个小单元中,并给这个小单页起一个名字,当用到这段代码时直接调用这个小单元的名字即可。

格式: function_name() {

           command
       }

函数必须要放在最前面

示例1

#!/bin/bash
function inp(){
echo $1 $2 $# $0

}
inp 1 a b

示例2

#!/bin/bash
sum(){
s=$[$1+$2]
echo $s
}
sum 1 10

示例3
#!/bin/bash
ip(){
ifconfig |grep -A1 "$1"|grep inet|awk '{print $2}'
}

read -p "Please input the eth name:" eth
myip=ip $eth
echo "$eth addresss is $myip"

shell中的数组

定义数组 a=(1 2 3 4 5); echo ${a[@]} echo ${a[]}
echo ${#a[@]} 获取数组的元素个数
echo ${a[2]} 读取第三个元素,数组从0开始
echo ${a[
]} 等同于 ${a[@]} 显示整个数组
数组赋值
a[1]=100; echo ${a[@]}
a[5]=2; echo ${a[@]} 如果下标不存在则会自动添加一个元素
数组的删除
uset a; unset a[1]

数组分片
a=(seq 1 5)
echo ${a[@]:0:3} 从第一个元素开始,截取3个
echo ${a[@]:1:4} 从第二个元素开始,截取4个
echo ${a[@]:0-3:2} 从倒数第3个元素开始,截取2个
数组替换
echo ${a[@]/3/100}
a=(${a[@]/3/100})

shell项目-告警系统
需求:使用shell定制各种个性化告警工具,但需要统一化管理、规范化管理。
思路:指定一个脚本包,包含主程序、子程序、配置文件、邮件引擎、输出日志等。
主程序:作为整个脚本的入口,是整个系统的命脉。
配置文件:是一个控制中心,用它来开关各个子程序,指定各个相关联的日志文件。
子程序:这个才是真正的监控脚本,用来监控各个指标。
邮件引擎:是由一个python程序来实现,它可以定义发邮件的服务器、发邮件人以及发件人密码
输出日志:整个监控系统要有日志输出。
shell编程知识

shell项目-告警系统main.sh
main.sh内容
#!/bin/bash
#Written by aming.

是否发送邮件的开关

export send=1 //子脚本需要过滤这个地方 1 发送 0不发送

过滤ip地址

export addr=ifconfig |grep -A1 "$1"|grep inet|awk '{print $2}' //获取主机地址
dir=pwd //获取当前目录bin目录

只需要最后一级目录名

last_dir=echo $dir|awk -F'/' '{print $NF}' //截取最后bin的最后一级目录

下面的判断目的是,保证执行脚本的时候,我们在bin目录里,不然监控脚本、邮件和日志很有可能找不到

if [ $last_dir == "bin" ] || [ $last_dir == "bin/" ]; then //判断最后一级是否为bin,是的话配置文件
conf_file="../conf/mon.conf"
else
echo "you shoud cd bin dir" //最后一级目录不是bin的时候 直接exit退出脚本
exit
fi
exec 1>>../log/mon.log 2>>../log/err.log //记录到日志
echo "date +"%F %T" load average" //打印负载时间
/bin/bash ../shares/load.sh //执行负载脚本
#先检查配置文件中是否需要监控502
if grep -q 'to_mon_502=1' $conf_file; then
export log=grep 'logfile=' $conf_file |awk -F '=' '{print $2}' |sed 's/ //g'
/bin/bash  ../shares/502.sh
fi

shell项目-告警系统mon.conf
mon.conf内容

to config the options if to monitor

定义mysql的服务器地址、端口以及user、password

to_mon_cdb=0   ##0 or 1, default 0,0 not monitor, 1 monitor
db_ip=10.20.3.13
db_port=3315
db_user=username
db_pass=passwd

httpd   如果是1则监控,为0不监控 //记录是否监控情况

to_mon_httpd=0

php 如果是1则监控,为0不监控

to_mon_php_socket=0

http_code_502  需要定义访问日志的路径

to_mon_502=1
logfile=/data/log/xxx.xxx.com/access.log

request_count   定义日志路径以及域名

to_mon_request_count=0
req_log=/data/log/www.discuz.net/access.log
domainname=www.discuz.net

shell项目-告警系统load.sh
load.sh内容
#! /bin/bash
##Writen by aming##
load=uptime |awk -F 'average:' '{print $2}'|cut -d',' -f1|sed 's/ //g' |cut -d. -f1 //过滤出负载情况
if [ $load -gt 10 ] && [ $send -eq "1" ] //负载大于规定之间且允许发送的时候执行mail脚本
then
echo "$addr date +%T load is $load" >../log/load.tmp //写入日志,方便发送邮件内容
/bin/bash ../mail/mail.sh aming_test@163.com "$addr_load:$load" cat ../log/load.tmp 发送邮件
fi
echo "date +%T load is $load"

shell项目-告警系统502.sh
502.sh内容
#! /bin/bash
d=date -d "-1 min" +%H:%M //一分钟前, 主程序是1分钟执行一次
c_502=grep :$d:  $log  |grep ' 502 '|wc -l //过滤502情况
if [ $c_502 -gt 10 ] && [ $send == 1 ]; then //大于规定次数且允许发送的时候 往下进行
     echo "$addr $d 502 count is $c_502">../log/502.tmp //写入日志
     /bin/bash ../mail/mail.sh $addr_502 $c_502  ../log/502.tmp 执行脚本发送
fi
echo "date +%T 502 $c_502"

shell项目-告警系统disk.sh

disk.sh内容
#! /bin/bash
##Writen by aming##
rm -f ../log/disk.tmp //删除上一次写入的日志
for r in df -h |awk -F '[ %]+' '{print $5}'|grep -v Use //awk 可以用多个分隔符
do
    if [ $r -gt 90 ] && [ $send -eq "1" ] //磁盘用量大于90且允许发送邮件
then
    echo "$addr date +%T disk useage is $r" >>../log/disk.tmp //打印日志到log
fi
if [ -f ../log/disk.tmp ] //判断日志是否存在
then
    df -h >> ../log/disk.tmp //将当前的磁盘情况打印到日志
    /bin/bash ../mail/mail.sh $addr_disk $r ../log/disk.tmp 执行脚本发送邮件
    echo "date +%T disk useage is nook"
else
    echo "date +%T disk useage is ok" //log不存在的时候,磁盘是正常的
fi

shell项目-告警系统mail.sh

mail.sh内容 //其中mail.py内容到这里下载https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D22Z/mail.py
  log=$1 //监控项
t_s=date +%s //时间戳1
t_s2=date -d "2 hours ago" +%s //两小时前的时间戳
if [ ! -f /tmp/$log ]
then //日志不存在的时候讲两小时前的时间戳,计入日志
echo $t_s2 > /tmp/$log
fi
t_s2=tail -1 /tmp/$log|awk '{print $1}' // 将第二个时间戳 过滤出来
echo $t_s>>/tmp/$log // 将此时的时间戳1 写入日志
v=$[$t_s-$t_s2] //两个时间戳之差
echo $v
if [ $v -gt 3600 ] //第一次执行的时候肯定大于3600,执行发送邮件
then
./mail.py $1 $2 $3 //第一次发送邮件
echo "0" > /tmp/$log.txt
else
if [ ! -f /tmp/$log.txt ] //第二次执行的时候,计数到日志
then
echo "0" > /tmp/$log.txt
fi
nu=cat /tmp/$log.txt
nu2=$[$nu+1]
echo $nu2>/tmp/$log.txt //将次数写入日志
if [ $nu2 -gt 10 ] //判断10次后是否恢复(一分钟执行一次,10次相当于10分钟)
then
./mail.py $1 "trouble continue 10 min $2" "$3" //超过十分钟,再次发邮件
echo "0" > /tmp/$log.txt //邮件发送完,归零
fi
fi  

shell分发系统介绍

expect脚本远程登录

yum install -y expect
自动远程登录
#! /usr/bin/expect
set host "192.168.133.132" //变量 host
set passwd "123456" //变量passwd
spawn ssh root@$host
expect {
"yes/no" { send "yes\r"; exp_continue}
"password:" { send "$passwd\r" }
}
interact

exptct脚本远程执行命令
自动远程登录后,执行命令并退出
#!/usr/bin/expect
set user "root"
set passwd "123456"
spawn ssh $user@192.168.133.132

expect {
"yes/no" { send "yes\r"; exp_continue}
"password:" { send "$passwd\r" }
}
expect "]"
send "touch /tmp/12.txt\r"
expect "]
"
send "echo 1212 > /tmp/12.txt\r"
expect "]*"
send "exit\r"

expect脚本传递参数
传递参数
#!/usr/bin/expect

set user [lindex $argv 0]
set host [lindex $argv 1]
set passwd "123456"
set cm [lindex $argv 2]
spawn ssh $user@$host

expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect "]"
send "$cm\r"
expect "]
"
send "exit\r"

expect脚本同步文件
自动同步文件
#!/usr/bin/expect
set passwd "123456"
spawn rsync -av root@192.168.133.132:/tmp/12.txt /tmp/
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof

expect脚本指定host和list
指定host和要同步的文件
#!/usr/bin/expect
set passwd "123456"
set host [lindex $argv 0]
set file [lindex $argv 1]
spawn rsync -av $file root@$host:$file
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof

构建文件分发系统

需求背景对于大公司而言,肯定时不时会有网站或者配置文件更新,而且使用的机器肯定也是好多台,少则几台,多则几十甚至上百台。所以,自动同步文件是至关重要的。
实现思路首先要有一台模板机器,把要分发的文件准备好,然后只要使用expect脚本批量把需要同步的文件分发到目标机器即可。
核心命令rsync -av --files-from=list.txt  /  root@host:/

文件分发系统的实现
rsync.expect 内容
#!/usr/bin/expect
set passwd "123456"
set host [lindex $argv 0]
set file [lindex $argv 1]
spawn rsync -av --files-from=$file / root@$host:/
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof
ip.list内容
192.168.133.132
192.168.133.133

批量远程执行命令

rsync.sh 内容
#!/bin/bash
for ip in cat ip.list
do
    echo $ip
    ./rsync.expect $ip list.txt
done

exe.expect 内容
#!/usr/bin/expect
set host [lindex $argv 0]
set passwd "123456"
set cm [lindex $argv 1]
spawn ssh root@$host
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect "]"
send "$cm\r"
expect "]
"
send "exit\r"

exe.sh 内容
#!/bin/bash
for ip in cat ip.list
do
    echo $ip
    ./exe.expect $ip "w;free -m;ls /tmp"
done

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值