跳转至

6. while循环

6.1 while循环

while [ 1 -gt 2 ];do echo hello;sleep 1;done

6.1.1 脚本放入后台执行

vim while_uptime.sh
#!/bin/bash
while true
do 
    uptime && uptime >>/tmp/uptime.log
    sleep 2
done

后台执行脚本

bash while_uptime.sh &

查看后台脚本进程号

#方法1
ps -ef |grep while_uptime.sh
#方法2
jobs -l

后台程序放入前台

fg 1

疑问:前台正在执行的程序,能在不中断的情况下放入后台吗?

6.1.2 循环打印数字

vim while_num.sh
#!/bin/bash
num=5
while [ "$num" -gt 0 ]
do
    echo "${num}"
    ((num--))
done

while条件还可以使用 (()) [[]]

while (( num > 0 ))
while [[ $num > 0 ]]

6.1.3 模拟seq脚本

模拟:seq 10 -1 1 
vim while_num.sh
#!/bin/bash
num="$1"
while [[ "$num" > 0 ]]
do
    echo "$num"
    ((num--))
done
bash while_num.sh 10

6.2 until循环

vim until_num.sh

#!/bin/bash
num="$1"
until [ $num -lt 1 ]
do
    echo "$num"
    ((num--))
done    

6.3 while 求和

#!/bin/bash
num=1
sum=0
while [ $num -le 100 ]
do
    ((sum=sum+num))
    ((num++))
done
[ "$sum" -ne 0 ] && printf "1+2+..100= $sum\n"

6.4 while监控网站

#!/bin/bash
usage(){
    echo "Usage: $0 url"
    exit 1
}

if [ $# -ne 1 ];then
    usage
fi

while true
    do
        if [ `curl -o /dev/null --connect-timeout 5 -s -w "%{http_code}" $1|egrep -w "200|301|302" |wc -l` -ne 1 ];then
            echo "$1 error..."
        else
            echo "$1 is working "
        fi
        sleep 10
    done

6.5 while检测多个url

#!/bin/bash

url_list=(
    www.pythonav.cn
    www.pythonav.com
    www.taobao.com
)

wait(){
    echo "3 seconds later starting check url..."
    echo ""
    for i in $(seq 3 -1 1)
        do 
            echo "$i"
            sleep 1
        done
    echo "----------------program now is running --------------"
}

check_url(){
    wait
    for ((i=0;i<`echo ${#url_list[*]}`;i++))
    do
        wget -o /dev/null -T 3 --tries=1 --spider ${url_list[$i]} >/dev/null 2>&1
        if [ $? -eq 0 ];then
            echo "${url_list[$i]} is working!"
        else
            echo "${url_list[$i]} is error!"
        fi
    done
    ((check_count++))
}

main(){
    while true
        do 
            check_url
            echo "--------------------check times :${check_count}--------"
            sleep 3
        done
}

main

6.6 while分析nginx日志脚本

exec echo "welcome to my linux classroom"

模拟cat命令

vim exec_file.sh
#!/bin/bash
exec < /var/log/nginx/access.log
while read line
    do 
        echo $line
    done

脚本

vim count_nginx_log.sh

#!/bin/bash
sum=0
exec<$1
while read line
do
    size=`echo $line|awk '{print $10}'`
    expr $size +1 &>/dev/null
    if [ $? -ne 0 ];then
        continue
    fi
    ((sum=sum+$size))
done
echo "$1 total data are: `echo $((${sum}/1024/1024))`MB"

6.7 防ddos攻击脚本

cat /var/log/nginx/access.log |awk '{print $1}'|grep -v "^$"|sort|uniq -c

脚本

#!/bin/bash
file=$1
while true
do
    awk '{print $1}' ${file}|grep -v "^$"|sort |uniq -c >/tmp/my_access_ip.log
    exec < /tmp/my_access_ip.log
    while read line
    do
        ip=`echo $line|awk '{print $2}'`
        count=`echo $line|awk '{print $1}'`
        if [ $count -gt 500  ] && [ `iptables -L -n |grep "$ip"|wc -l` -lt 1 ];then
            iptables -I INPUT -s $ip -j DROP
            echo "$line is dropped" >/tmp/drop_list_$(date +%F).log
        fi
    done
    sleep 3600
done

我的json格式日志处理

root@vpn:/shell-scripts# head -1 /var/log/nginx/access.log
{"@timestamp":"05/Jan/2022:00:22:09 +0000","N_client_ip": "135.125.244.48","N_request": "POST / HTTP/1.1","N_request_time": "0.000","N_status": "405","N_bytes": "568","N_user_agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36","N_x_forwarded": "-","N_referer": "-"}
root@vpn:/shell-scripts#
root@vpn:/shell-scripts# cat /var/log/nginx/access.log |awk -F "," '{print $2}'|awk -F ":" '{print $2}'|awk -F '"' '{print $2}'|sort |uniq -c|sort -r
      8 135.125.244.48
      6 45.146.165.37
      4 159.223.161.250
      2 3.229.135.132
      2 205.185.122.184
      2 185.254.196.218
      2 185.254.196.217
      2 159.223.161.254
      2 159.223.161.190
      2 143.244.189.0
      2 143.110.227.92
      2 122.5.31.182
      1 93.116.232.13
      1 66.240.205.34
      1 43.129.40.155
      1 23.251.102.74
      1 209.17.96.98
      1 209.141.53.74
      1 205.185.114.93
      1 196.11.178.136
      1 180.215.197.133
      1 179.49.117.234
      1 178.212.55.34
      1 172.111.36.142
      1 165.255.253.245
      1 136.144.41.6
      1 124.41.214.106
      1 104.206.128.30
      1 103.79.167.236
      1 103.145.13.223
      1 103.114.158.1

最后更新: 2022-02-19 13:59:07