Explication de la programmation du script Day2-Shell (ob14) -1

I. Expliquer en détail les conditions 05-if

Syntaxe de structure de branche unique:

if [ 条件 ];then
	指令
fi

Syntaxe de la structure multi-branches:

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

Déterminer si le paramètre est un nombre entier

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

si [-n str1] est vrai lorsque la longueur de la chaîne est supérieure à 0 (la chaîne n'est pas vide). S'il s'agit d'un nombre, sed sera une valeur nulle après le remplacement.
Insérez la description de l'image ici

Vérifiez si le service mysql existe

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" [email protected] < $LOG_FILE  #将上面的Status发到邮箱
fi
exit $RETVAL

Surveiller le service Apache (vérifier le port, le processus et l'URL en même temps)

#先加载 /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" [email protected] <$LOG_FILE
      exit
else
    action "httpd is running" /bin/true
    exit 0
fi

Insérez la description de l'image ici

Surveiller si un port est normal

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."

Insérez la description de l'image ici

2. Expliquez en détail les conditions du cas 06

structure de cas grammaire conditionnelle

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

Démarrer / arrêter / redémarrer le script apache (cas d'utilisation)

Ces deux fichiers sont les mêmes, en fait l'un est un lien souple
Insérez la description de l'image ici
Insérez la description de l'image ici

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

Insérez la description de l'image ici

#!/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

Insérez la description de l'image ici

Trois, en détail boucle 07-while

Alors que la syntaxe des phrases avec sursis:

while 条件
	do
	指令
done

Connaissance de l'exécution en arrière-plan des scripts

Insérez la description de l'image ici
Insérez la description de l'image ici

Vérifiez l'état de l'url du site Web

#!/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

Insérez la description de l'image ici

Calculez la somme du nombre d'octets d'accès de chaque élément de journal de toutes les lignes du journal apache access_2010-12-8.log pour un jour.

Contenu du journal:

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)"

Insérez la description de l'image ici

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"

Insérez la description de l'image ici

FINIR

Je suppose que tu aimes

Origine blog.csdn.net/Nightwish5/article/details/113464397
conseillé
Classement