day2-Shell脚本编程精讲(ob14)-1

一、精讲05-if条件

单分支结构语法:

if [ 条件 ];then
	指令
fi

多分支结构语法:

if 条件
	then
	指令
elif 条件
	then	
	指令
else
	指令
fi

判断参数是否为整型数字

#judge num
[ -n "`echo $1|sed 's/[0-9]//g'`" -a -n "`echo $2|sed 's/[0-9]//g'`"] &&\
echo "两个参数都必须为数字" && exit

if [ -n str1 ]       当串的长度大于0时为真(串非空) 。如个是数字,则sed替换后是空值。
在这里插入图片描述

检测判断mysql服务是否存在

MYUSER=root
MYPASS="oldboy"
MYSOCK=/data/3306/mysql.sock
MySQL_STARTUP="/data/3306/mysql"
LOG_PATH=/tmp
LOG_FILE=${LOG_PATH}/mysqllogs_`date +%F`.log
MYSQL_PATH=/usr/local/mysql/bin
MYSQL_CMD="$MYSQL_PATH/mysql -u$MYUSER -p$MYPASS -S $MYSOCK"
$MYSQL_CMD -e "select version();" >/dev/null 2>&1
if [ $? -eq 0 ] 
then
	echo "MySQL is running! " 
	exit 0
else
    $MySQL_STARTUP start >$LOG_FILE
    sleep 5;
    $MYSQL_CMD -e "select version();" >/dev/null 2>&1
    if [ $? -ne 0 ]
      then 
      	 #通过for循环来杀mysqld  ,然后启动mysqld
        for num in `seq 5`
        do
           killall mysqld  >/dev/null 2>&1 
	   [ $? -ne 0 ] && break;
	       sleep 1
		done
        $MySQL_STARTUP start >>$LOG_FILE 
    fi
    $MYSQL_CMD -e "select version();" >/dev/null 2>&1 && Status="restarted" || Status="unknown"
    mail -s "MySQL status is $Status" 6xxxxx1@qq.com < $LOG_FILE  #将上面的Status发到邮箱
fi
exit $RETVAL

监控apache服务(对端口,进程,URL同时检测)

#先加载 /etc/init.d/functions 可参考https://www.cnblogs.com/sunfie/p/5149678.html
. /etc/init.d/functions
LOG_FILE=/tmp/httpd.log
apachectl="/application/apache/bin/apachectl"
HTTPPORTNUM=`netstat -lnt|grep 80|grep -v grep|wc -l`
HTTPPRONUM=`ps -ef|grep http|grep -v grep|wc -l`
#使用 wget –spider 测试下载链接 ,如果下载链接正确,将会显示内容
#可参考https://www.cnblogs.com/sx66/p/11887022.html
wget --quiet --spider http://10.0.0.179:8000 && RETVAL=$?
#以上变量要事先调试正确后 在放到脚本运行
[ ! -f $LOG_FILE ] && touch $LOG_FILE 

#不正常情况的判断
if [ "$RETVAL" != "0" -o "$HTTPPORTNUM" -lt "1"  -o  "$HTTPPRONUM" \< "1" ] ;then
#echo $RETVAL $HTTPPORTNUM $HTTPPRONUM
#exit
   action "httpd is not running" /bin/false
   echo -e "httpd is not running\n" >$LOG_FILE
   #杀进程,然后启动
   echo "Preparing start apache..."
        for num in `seq 10`
         do   
            killall httpd  >/dev/null 2>&1 
            [ $? -ne 0 ] && {
              echo "httpd is killed" >$LOG_FILE
              break;
	     }
	    sleep 2
	 done
     $apachectl start >/dev/null 2>&1 && Status="started" || Status="unknown" 
     [ "$Status" = "started" ] && action "httpd is started" /bin/true||\
      action "httpd is started" /bin/false
      mail -s "`uname -n`'s httpd status is $Status" xxxxxx1@qq.com <$LOG_FILE
      exit
else
    action "httpd is running" /bin/true
    exit 0
fi

在这里插入图片描述

监控某个端口是否正常

ip_add="$1" 
port="$2"
print_usage(){
	   echo -e "$0 ip port"
	   exit 1
}
		
#judge para num
if [ $# -ne 2 ]
	then
		print_usage
fi
#使用nmap 来对端口进行扫描
PORT_COUNT=`nmap $ip_add  -p $port|grep open|wc -l`
#echo -e "\n" |telnet $ip_add $port||grep Connected
#echo -e "\n"|telnet 10.0.0.179 8000|grep Connected
[[ $PORT_COUNT -ge 1 ]] && echo "$ip_add $port is ok." || echo "$ip_add $port is unknown."

在这里插入图片描述

二、精讲06-case条件

case结构条件语法

case "字符串" in
	值1) 指令...
;;
	值1) 指令...
;;
	*) 指令...
esac

启动/关闭/重启apache脚本 (用case)

这两个文件是一样的,其实有个是软连接
在这里插入图片描述
在这里插入图片描述

#关闭apache
/appilication/apache/bin/httpd -k stop
#开启apache
/appilication/apache/bin/httpd -k start

在这里插入图片描述

#!/bin/bash
# Source function library.
[ -f /etc/rc.d/init.d/functions ] && . /etc/rc.d/init.d/functions
RETVAL=0
httpd="/application/apache/bin/httpd"
start() {
        $httpd -k start >/dev/null 2>&1
        RETVAL=$?
        [ $RETVAL -eq 0 ] && action "启动 httpd:" /bin/true ||\
        action "启动 httpd:" /bin/false
        return $RETVAL
}

stop() {
        $httpd -k stop >/dev/null 2>&1
        [ $? -eq 0 ] && action "停止 httpd:" /bin/true ||\
        action "停止 httpd:" /bin/false
        return $RETVAL
}
case "$1" in
  start)
        start
		;;
  stop)
        stop
        ;;
  restart)
       
       sh $0 stop
       sh $0 start
        ;;
   *)
        echo "Format error!"
        echo $"Usage: $0 {start|stop|restart}"
        exit 1
        ;;
esac
exit $RETVAL

在这里插入图片描述

三、精讲07-while循环

while条件句语法:

while 条件
	do
	指令
done

脚本后台执行的知识

在这里插入图片描述
在这里插入图片描述

检查网站url的状态

#!/bin/bash
. /etc/init.d/functions
#将要检测的url放到数组中
url_list=(
https://www.baidu.com/
https://www.qq.com/
https://www.4399.com/
)

function wait()
{
echo -n '3秒后,执行该操作.';
for ((i=0;i<3;i++))
do
  echo -n ".";sleep 1
done
echo
}

function check_url()
{
wait
echo 'check url...'
for ((i=0; i<`echo ${#url_list[*]}`; i++))
do
#结果为 HTTP/1.1 200 OK   目前只是检测200状态的,没添加重定向301、302的判断
#if [[ "${judge[1]}" == '200' || "${judge[1]}" == '301'  && "${judge[2]}"=='OK' ]]
judge=($(curl -I -s ${url_list[$i]}|head -1|tr "\r" "\n"))
if [[ "${judge[1]}" == '200' && "${judge[2]}"=='OK' ]]
   then
   action "${url_list[$i]}" /bin/true
else
   action "${url_list[$i]}" /bin/false
fi
done
}

check_url

在这里插入图片描述

计算 apache一天的日志access_2010-12-8.log中所有行的日志各元素的访问字节数的总和。

日志内容:

59.33.26.105 - - [08/Dec/2010:15:43:55 +0800] "GET //back/upload/course/2010-10-25-23-48-59-048-18.jpg HTTP/1.1" 200 44286 "http://oldboy.blog.51cto.com/static/web/column/17/index.shtml?courseId=43" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"
59.33.26.105 - - [08/Dec/2010:15:43:55 +0800] "GET /back/upload/teacher/2010-08-06-11-39-59-0469.jpg HTTP/1.1" 200 10850 "http://oldboy.blog.51cto.com/static/web/column/17/index.shtml?courseId=43" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"
59.33.26.105 - - [08/Dec/2010:15:43:55 +0800] "GET /back/upload/teacher/2010-08-30-13-57-43-06210.jpg HTTP/1.1" 200 11809 "http://oldboy.blog.51cto.com/static/web/column/17/index.shtml?courseId=43" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"
59.33.26.105 - - [08/Dec/2010:15:43:55 +0800] "GET /static/web/coursesort/5.shtml HTTP/1.1" 200 255 "http://oldboy.blog.51cto.com/static/web/column/17/index.shtml?courseId=43" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"
59.33.26.105 - - [08/Dec/2010:15:43:55 +0800] "POST /cms/cmtweb!getCommentListBySource.action HTTP/1.1" 200 433 "http://oldboy.blog.51cto.com/static/web/column/17/index.shtml?courseId=43" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"
59.33.26.105 - - [08/Dec/2010:15:43:56 +0800] "GET /static/images/photos/2.jpg HTTP/1.1" 200 11299 "http://oldboy.blog.51cto.com/static/web/column/17/index.shtml?courseId=43" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"
59.33.26.105 - - [08/Dec/2010:15:43:56 +0800] "GET /static/images/photos/2.jpg HTTP/1.1" 200 11299 "http://oldboy.blog.51cto.com/static/web/column/17/index.shtml?courseId=43" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"
59.33.26.105 - - [08/Dec/2010:15:43:56 +0800] "GET /static/images/photos/2.jpg HTTP/1.1" 200 11299 "http://oldboy.blog.51cto.com/static/web/column/17/index.shtml?courseId=43" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"
59.33.26.105 - - [08/Dec/2010:15:44:02 +0800] "GET /static/flex/vedioLoading.swf HTTP/1.1" 200 3583 "http://oldboy.blog.51cto.com/static/flex/AdobeVideoPlayer.swf?width=590&height=328&url=/[[DYNAMIC]]/2" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"
124.115.4.18 - - [08/Dec/2010:15:44:15 +0800] "GET /?= HTTP/1.1" 200 46232 "-" "-"
124.115.4.18 - - [08/Dec/2010:15:44:25 +0800] "GET /static/js/web_js.js HTTP/1.1" 200 4460 "-" "-"
124.115.4.18 - - [08/Dec/2010:15:44:25 +0800] "GET /static/js/jquery.lazyload.js HTTP/1.1" 200 1627 "-" "-"
124.115.4.18 - - [08/Dec/2010:15:44:25 +0800] "GET /static/js/common.js HTTP/1.1" 200 1861 "-" "-"
124.115.4.18 - - [08/Dec/2010:15:44:25 +0800] "GET /static/js/default.js HTTP/1.1" 200 2686 "-" "-"
124.115.4.18 - - [08/Dec/2010:15:44:25 +0800] "GET /static/js/cookieUtil.js HTTP/1.1" 200 955 "-" "-"
124.115.4.18 - - [08/Dec/2010:15:44:25 +0800] "GET /static/js/jquery-jquery-1.3.2.min.js HTTP/1.1" 200 57254 "-" "-"
124.115.4.18 - - [08/Dec/2010:15:44:26 +0800] "GET /static/js/addToCart.js HTTP/1.1" 200 6417 "-" "-"
123.122.65.226 - - [08/Dec/2010:15:44:43 +0800] "GET /static/flex/vedioLoading.swf HTTP/1.1" 304 - "http://oldboy.blog.51cto.com/static/flex/VideoCenter.swf?url=/[[DYNAMIC]]/2" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"
123.122.65.226 - - [08/Dec/2010:15:44:43 +0800] "POST /messagebroker/amf HTTP/1.1" 200 183 "http://oldboy.blog.51cto.com/static/flex/VideoCenter.swf?url=/[[DYNAMIC]]/4" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"
123.122.65.226 - - [08/Dec/2010:15:44:43 +0800] "POST /messagebroker/amf HTTP/1.1" 200 117 "http://oldboy.blog.51cto.com/static/flex/VideoCenter.swf?url=/[[DYNAMIC]]/4" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"

在这里插入图片描述

exec <$1
sum=0
while read line
do
  num=`echo $line|awk '{print $10}'`
  [ -n "$num" -a "$num" = "${num//[^0-9]/}" ] || continue
  ((sum=sum+$num))
done
  echo "${1}:${sum} bytes =`echo $((${sum}/1024))`KB"

在这里插入图片描述

END

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值