36.第六章 Shell脚本编程高级进阶(一)

2021/11/7 7:15:41

本文主要是介绍36.第六章 Shell脚本编程高级进阶(一),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

4.2.4 while 循环

格式:

while COMMANDS; do COMMANDS; done

while CONDITION; do
    循环体
done

说明:

CONDITION:循环控制条件;进入循环之前,先做一次判断;每一次循环之后会再次做判断;条件为“true”,则执行一次循环;直到条件测试状态为“false”终止循环,因此:CONDTION一般应该有循环控制变量;而此变量的值会在循环体不断地被修正

进入条件:CONDITION为true

退出条件:CONDITION为false

无限循环

while true; do
    循环体
done

范例:

[root@rocky8 bin]# type while
while is a shell keyword
[root@rocky8 bin]# help while
while: while COMMANDS; do COMMANDS; done
    Execute commands as long as a test succeeds.
    
    Expand and execute COMMANDS as long as the final command in the
    `while' COMMANDS has an exit status of zero.
    
    Exit Status:
    Returns the status of the last command executed.

在这里插入图片描述

范例:

[root@rocky8 bin]# help :
:: :
    Null command.
    
    No effect; the command does nothing.
    
    Exit Status:
    Always succeeds.
[root@rocky8 bin]# type true
true is a shell builtin
[root@rocky8 bin]# help true
true: true
    Return a successful result.
    
    Exit Status:
    Always succeeds.
#: 和 true 是一样的 

范例:

[root@rocky8 ~]# vim while.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-21
#FileName:      while.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
i=1
sum=0
while [ $i -le 100 ];do
    let sum+=i
    let i++
done
echo $sum

[root@rocky8 ~]# bash while.sh 
5050

范例:

[root@rocky8 bin]# vim while_check_disk.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-10
#FileName:      while_check_disk.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
WARNING=80
while :;do
    USE=`df|sed -rn '/^\/dev\/sd/s#.* ([0-9]+)%.*#\1#p' |sort -nr |head -1`
    if [ $USE -gt $WARNING ];then
        echo Disk will be full from `hostname -I` | mail -s "disk warning" 88563128@qq.com
    fi
    sleep 10
done

[root@rocky8 bin]# bash while_check_disk.sh

[root@rocky8 ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        374M     0  374M   0% /dev
tmpfs           392M     0  392M   0% /dev/shm
tmpfs           392M  5.6M  386M   2% /run
tmpfs           392M     0  392M   0% /sys/fs/cgroup
/dev/sda2       100G  2.3G   98G   3% /
/dev/sda3        50G  392M   50G   1% /data
/dev/sda1      1014M  188M  827M  19% /boot
tmpfs            79M     0   79M   0% /run/user/0
[root@rocky8 ~]# dd if=/dev/zero of=/boot/f1.img bs=1M count=800
800+0 records in
800+0 records out
838860800 bytes (839 MB, 800 MiB) copied, 1.42045 s, 591 MB/s
[root@rocky8 ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        374M     0  374M   0% /dev
tmpfs           392M     0  392M   0% /dev/shm
tmpfs           392M  5.6M  386M   2% /run
tmpfs           392M     0  392M   0% /sys/fs/cgroup
/dev/sda2       100G  2.3G   98G   3% /
/dev/sda3        50G  392M   50G   1% /data
/dev/sda1      1014M  988M   27M  98% /boot
tmpfs            79M     0   79M   0% /run/user/0
#磁盘空间大于80%,邮箱会收到邮件

[root@rocky8 ~]# rm -f /boot/f1.img 
[root@rocky8 ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        374M     0  374M   0% /dev
tmpfs           392M     0  392M   0% /dev/shm
tmpfs           392M  5.6M  386M   2% /run
tmpfs           392M     0  392M   0% /sys/fs/cgroup
/dev/sda2       100G  2.3G   98G   3% /
/dev/sda3        50G  392M   50G   1% /data
/dev/sda1      1014M  188M  827M  19% /boot
tmpfs            79M     0   79M   0% /run/user/0

范例: 九九乘法表

[root@rocky8 ~]# vim while_99.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-10
#FileName:      while_99.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
i=1
while [ $i -le 9 ];do
    j=1
    while [ $j -le $i ];do
        echo -e "${j}x${i}=$[j*i]\t\c"
        let j++                                                                                               
    done
    echo
    let i++
done

[root@rocky8 ~]# bash while_99.sh 
1x1=1	
1x2=2	2x2=4	
1x3=3	2x3=6	3x3=9	
1x4=4	2x4=8	3x4=12	4x4=16	
1x5=5	2x5=10	3x5=15	4x5=20	5x5=25	
1x6=6	2x6=12	3x6=18	4x6=24	5x6=30	6x6=36	
1x7=7	2x7=14	3x7=21	4x7=28	5x7=35	6x7=42	7x7=49	
1x8=8	2x8=16	3x8=24	4x8=32	5x8=40	6x8=48	7x8=56	8x8=64	
1x9=9	2x9=18	3x9=27	4x9=36	5x9=45	6x9=54	7x9=63	8x9=72	9x9=81	

4.2.5 练习:用while实现

1、编写脚本,求100以内所有正奇数之和
2、编写脚本,提示请输入网络地址,如192.168.0.0,判断输入的网段中主机在线状态,并统计在线和离线主机各多少
3、编写脚本,打印九九乘法表
4、编写脚本,利用变量RANDOM生成10个随机数字,输出这个10数字,并显示其中的最大值和最小值
5、编写脚本,实现打印国际象棋棋盘
6、后续六个字符串:efbaf275cd、4be9c40b8b、44b2395c46、f8c8873ce0、b902c16c8b、ad865d2f63是通过对随机数变量RANDOM随机执行命令: echo $RANDOM|md5sum|cut -c1-10 后的结果,请破解这些字符串对应的RANDOM值

4.2.6 until 循环

在这里插入图片描述

格式:

until COMMANDS; do COMMANDS; done

until CONDITION; do
    循环体
done

说明:

进入条件: CONDITION 为false

退出条件: CONDITION 为true

无限循环

until false; do
    循环体
Done

范例:

[root@rocky8 ~]# type until
until is a shell keyword
[root@rocky8 ~]# help until
until: until COMMANDS; do COMMANDS; done
    Execute commands as long as a test does not succeed.
    
    Expand and execute COMMANDS as long as the final command in the
    `until' COMMANDS has an exit status which is not zero.
    
    Exit Status:
    Returns the status of the last command executed.

[root@rocky8 ~]# type false
false is a shell builtin
[root@rocky8 ~]# help false
false: false
    Return an unsuccessful result.
    
    Exit Status:
    Always fails.

范例:

[root@rocky8 ~]# useradd admin; echo 123456 |passwd --stdin admin
Changing password for user admin.
passwd: all authentication tokens updated successfully.

[root@centos7 ~]# ssh admin@172.31.1.8
The authenticity of host '172.31.1.8 (172.31.1.8)' can't be established.
ECDSA key fingerprint is SHA256:bSzdLQOWD7YlTznUn4y0YXH3WN9c11zb/0wqZRE/aHU.
ECDSA key fingerprint is MD5:9b:4f:8b:8e:de:63:fa:7b:7c:b2:a5:f7:da:0b:ed:9f.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.31.1.8' (ECDSA) to the list of known hosts.
admin@172.31.1.8's password: 
[admin@rocky8 ~]$ 

[root@rocky8 bin]# who |grep -q "^admin\>"
[root@rocky8 bin]# echo $?
0

[admin@rocky8 ~]$ exit
logout
Connection to 172.31.1.8 closed.
[root@centos7 ~]#
[root@rocky8 bin]# who |grep -q "^admin\>"
[root@rocky8 bin]# echo $?
1

范例:

[root@rocky8 bin]# vim until_hacker.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-10
#FileName:      until_hacker.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
until who |grep -q "^admin\>" ;do
    sleep 10
done
echo hacker login at `date +"%F %T"` |mail -s warning 88563128@qq.com

[root@rocky8 bin]# bash until_hacker.sh 
[root@centos7 ~]# ssh admin@172.31.1.8
admin@172.31.1.8's password: 
Last login: Sun Oct 10 16:29:37 2021 from 172.31.0.7
[admin@rocky8 ~]$ 
#登录就发邮件

[root@rocky8 bin]# vim until_hacker2.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-10
#FileName:      until_hacker2.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
until false ;do
    who |grep -q "^admin\>" && echo hacker login at `date +"%F %T"` |mail -s warning 88563128@qq.com
    sleep 10
done

[root@rocky8 bin]# bash until_hacker2.sh
[root@centos7 ~]# ssh admin@172.31.1.8
admin@172.31.1.8's password: 
Last login: Sun Oct 10 16:33:05 2021 from 172.31.0.7
[admin@rocky8 ~]$ 
[admin@rocky8 ~]$ exit
logout
Connection to 172.31.1.8 closed.
#只要发现登录就一直发邮件

4.2.7 循环控制语句 continue

continue [N]:提前结束第N层的本轮循环,而直接进入下一轮判断;最内层为第1层

while CONDITION1; do
    CMD1
    ...
    if CONDITION2; then
        continue
    fi
    CMDn
    ...
done

范例:

[root@rocky8 ~]# vim continue_for.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-20
#FileName:      continue_for.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
for ((i=0;i<10;i++));do
    [ $i -eq 5 ] && continue
    echo $i
    echo ------------------------
done

[root@rocky8 ~]# bash continue_for.sh 
0
------------------------
1
------------------------
2
------------------------
3
------------------------
4
------------------------
6
------------------------
7
------------------------
8
------------------------
9
------------------------
# continue 结束本层循环

[root@rocky8 ~]# vim continue_for2.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-20
#FileName:      continue_for.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
for ((i=0;i<10;i++));do
    for ((j=0;j<10;j++));do
        #[ $j -eq 5 ] && continue 2
        echo j=$j
    done
    echo ------------------------
done

[root@rocky8 ~]# bash continue_for2.sh 
j=0
j=1
j=2
j=3
j=4
j=5
j=6
j=7
j=8
j=9
------------------------
j=0
j=1
j=2
j=3
j=4
j=5
j=6
j=7
j=8
j=9
------------------------
j=0
j=1
j=2
j=3
j=4
j=5
j=6
j=7
j=8
j=9
------------------------
j=0
j=1
j=2
j=3
j=4
j=5
j=6
j=7
j=8
j=9
------------------------
j=0
j=1
j=2
j=3
j=4
j=5
j=6
j=7
j=8
j=9
------------------------
j=0
j=1
j=2
j=3
j=4
j=5
j=6
j=7
j=8
j=9
------------------------
j=0
j=1
j=2
j=3
j=4
j=5
j=6
j=7
j=8
j=9
------------------------
j=0
j=1
j=2
j=3
j=4
j=5
j=6
j=7
j=8
j=9
------------------------
j=0
j=1
j=2
j=3
j=4
j=5
j=6
j=7
j=8
j=9
------------------------
j=0
j=1
j=2
j=3
j=4
j=5
j=6
j=7
j=8
j=9
------------------------

[root@rocky8 ~]# vim continue_for2.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-20
#FileName:      continue_for.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
for ((i=0;i<10;i++));do
    for ((j=0;j<10;j++));do
        [ $j -eq 5 ] && continue 2
        echo j=$j
    done
    echo ------------------------
done

[root@rocky8 ~]# bash continue_for2.sh 
j=0
j=1
j=2
j=3
j=4
j=0
j=1
j=2
j=3
j=4
j=0
j=1
j=2
j=3
j=4
j=0
j=1
j=2
j=3
j=4
j=0
j=1
j=2
j=3
j=4
j=0
j=1
j=2
j=3
j=4
j=0
j=1
j=2
j=3
j=4
j=0
j=1
j=2
j=3
j=4
j=0
j=1
j=2
j=3
j=4
j=0
j=1
j=2
j=3
j=4
#continue 2 结束上一层循环

4.2.8 循环控制语句 break

break [N]:提前结束第N层整个循环,最内层为第1层

范例:

[root@rocky8 ~]# vim break_for.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-20
#FileName:      continue_for.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
for ((i=0;i<10;i++));do
    for ((j=0;j<10;j++));do
        [ $j -eq 5 ] && break
        echo j=$j
    done
    echo ------------------------
done

[root@rocky8 ~]# bash break_for.sh 
j=0
j=1
j=2
j=3
j=4
------------------------
j=0
j=1
j=2
j=3
j=4
------------------------
j=0
j=1
j=2
j=3
j=4
------------------------
j=0
j=1
j=2
j=3
j=4
------------------------
j=0
j=1
j=2
j=3
j=4
------------------------
j=0
j=1
j=2
j=3
j=4
------------------------
j=0
j=1
j=2
j=3
j=4
------------------------
j=0
j=1
j=2
j=3
j=4
------------------------
j=0
j=1
j=2
j=3
j=4
------------------------
j=0
j=1
j=2
j=3
j=4
------------------------
#break 退出本层循环

[root@rocky8 ~]# vim break_for.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-20
#FileName:      continue_for.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
for ((i=0;i<10;i++));do
    for ((j=0;j<10;j++));do
        [ $j -eq 5 ] && break 2
        echo j=$j
    done
    echo ------------------------
done

[root@rocky8 ~]# bash break_for.sh 
j=0
j=1
j=2
j=3
j=4
#break 2 退出上一层循环

范例:

[root@rocky8 ~]# vim menu.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-20
#FileName:      menu.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
sum=0
COLOR='echo -e \033[1;31m'
COLOR2='echo -e \033[1;32m'
END="\033[0m"

while true;do
    echo -e "\033[33;1m\c"
    cat <<-EOF
1) 鲍鱼
2) 满汉全席
3) 龙虾
4) 燕窝
5) 帝王蟹
6) 退出
EOF
    echo -e "\033[0m"

    read -p "请点菜(1-6): " MENU
    case $MENU in
    1|4)
        $COLOR'菜价: $10'$END
        let sum+=10
        ;;
    3|5)
        $COLOR'菜价: $20'$END
        let sum+=20
        ;;
    2)
        $COLOR'菜价: $1000'$END
        let sum+=1000
        ;;
    6)
        $COLOR2"你点的菜总价格是:\$$sum"$END
        break
        ;;
    *)
        echo "点错了,没有这道菜"
        ;;
    esac
    $COLOR2"你点的菜总价格是:\$$sum"$END
done

[root@rocky8 ~]# bash menu.sh 
1) 鲍鱼
2) 满汉全席
3) 龙虾
4) 燕窝
5) 帝王蟹
6) 退出

请点菜(1-6): 1
菜价: $10
你点的菜总价格是:$10
1) 鲍鱼
2) 满汉全席
3) 龙虾
4) 燕窝
5) 帝王蟹
6) 退出

请点菜(1-6): 5
菜价: $20
你点的菜总价格是:$30
1) 鲍鱼
2) 满汉全席
3) 龙虾
4) 燕窝
5) 帝王蟹
6) 退出

请点菜(1-6): 6
你点的菜总价格是:$30

范例:

[root@rocky8 ~]# vim guess.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-20
#FileName:      guess.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
NUM=$[RANDOM%10]

while read -p "输入 0-9 之间的数字: " INPUT ;do
    if [ $INPUT -eq $NUM ];then
        echo "恭喜你猜对了!"
        break
    elif [ $INPUT -gt $NUM ];then
        echo "数字太大了,重新猜!"
    else
        echo "数字太小了,重新猜!"
    fi
done

[root@rocky8 ~]# bash guess.sh 
输入 0-9 之间的数字: 8
数字太大了,重新猜!
输入 0-9 之间的数字: 7
数字太大了,重新猜!
输入 0-9 之间的数字: 5
恭喜你猜对了!

4.2.9 循环控制 shift 命令

shift [n] 用于将参量列表 list 左移指定次数,缺省为左移一次。
参量列表 list 一旦被移动,最左端的那个参数就从列表中删除。while 循环遍历位置参量列表时,常用到 shift

范例:

[root@rocky8 ~]# vim createuser.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-20
#FileName:      createuser.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
while true;do
    [ -z "$1" ] && break
    useradd $1 && echo $1 is created
    shift
done

[root@rocky8 ~]# bash createuser.sh a b c d e f
a is created
b is created
c is created
d is created
e is created
f is created

[root@rocky8 ~]# vim for_createuser.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-20
#FileName:      for_createuser.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
for i in $*;do
    useradd $i && echo $i is created
done

[root@rocky8 ~]# bash for_createuser.sh user{1..5}
user1 is created
user2 is created
user3 is created
user4 is created
user5 is created

[root@rocky8 ~]# vim createuser2.sh
[root@rocky8 ~]# cat createuser2.sh 
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-20
#FileName:      createuser2.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
while [ $1 ];do
    useradd $1 && echo $1 is created
    shift
done

[root@rocky8 ~]# bash createuser2.sh user{6..10}
user6 is created
user7 is created
user8 is created
user9 is created
user10 is created

[root@rocky8 ~]# vim createuser3.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-20
#FileName:      createuser3.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************

if [ $# -eq 0 ];then
    echo "Usage: `basename $0` user1 user2 ..."
    exit
fi

while [ "$1" ];do
    if id $1 &> /dev/null;then
        echo $1 is exist
    else
        useradd $1
        echo "$1 is created"
    fi
    shift
done
echo "All user is created"

[root@rocky8 ~]# bash createuser3.sh 
Usage: createuser3.sh user1 user2 ...
[root@rocky8 ~]# bash createuser3.sh tom jack bob user{1..5}
tom is created
jack is created
bob is created
user1 is exist
user2 is exist
user3 is exist
user4 is exist
user5 is exist
All user is created

4.2.10 练习

1、每隔3秒钟到系统上获取已经登录的用户的信息;如果发现用户hacker登录,则将登录时间和主机记录于日志/var/log/login.log中,并退出脚本
2、随机生成10以内的数字,实现猜字游戏,提示比较大或小,相等则退出
3、用文件名做为参数,统计所有参数文件的总行数
4、用二个以上的数字为参数,显示其中的最大值和最小值

4.2.11 while read 特殊用法

while 循环的特殊用法,遍历文件或文本的每一行

格式:

while read line; do
    循环体
done < /PATH/FROM/SOMEFILE

说明:依次读取/PATH/FROM/SOMEFILE文件中的每一行,且将行赋值给变量line

范例:

[root@rocky8 ~]# read a
1
[root@rocky8 ~]# echo $a
1

[root@rocky8 ~]# while read name;do 
echo $name
done
zhang
zhang
wang
wang
li
li
^C

[root@rocky8 ~]# while read name age ;do echo name=$name;echo age=$age ; done
zhang 18
name=zhang
age=18
wang 20
name=wang
age=20
^C

[root@rocky8 ~]# echo raymond | read X ; echo $X

[root@rocky8 ~]# echo raymond | while read X ; do echo $X;done
raymond
[root@rocky8 ~]# echo raymond | { read X ; echo $X; }
raymond
[root@rocky8 ~]# echo raymond | ( read X ; echo $X )
raymond
[root@rocky8 ~]# echo zhang wang li | ( read X Y Z; echo $X $Y $Z )
zhang wang li
[root@rocky8 ~]# echo zhang wang li | while read X Y Z; do echo $X $Y $Z;done
zhang wang li


[root@rocky8 ~]# vim user.txt
zhang 123456
wang 555555
li 666666

[root@rocky8 ~]# while read name password ; do useradd $name && echo $name is created; echo $password| passwd --stdin $name &> /dev/null;done < user.txt 
zhang is created
wang is created
li is created

范例:

[root@rocky8 ~]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
devtmpfs          382688       0    382688   0% /dev
tmpfs             400580       0    400580   0% /dev/shm
tmpfs             400580    5688    394892   2% /run
tmpfs             400580       0    400580   0% /sys/fs/cgroup
/dev/sda2      104806400 2287884 102518516   3% /
/dev/sda3       52403200 1446976  50956224   3% /data
/dev/sda1        1038336  191796    846540  19% /boot
tmpfs              80116       0     80116   0% /run/user/0
[root@rocky8 ~]# df|sed -rn '/^\/dev\/sd/s#^([^ ]+).* ([0-9]+)%.*#\1 \2#p'
/dev/sda2 3
/dev/sda3 3
/dev/sda1 19

[root@rocky8 ~]# vim disk_check.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-20
#FileName:      disk_check.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
WARNING=80
df|sed -rn '/^\/dev\/sd/s#^([^ ]+).* ([0-9]+)%.*#\1 \2#p' |while read part use ;do
    [ $use -ge $WARNING ] && echo "$part will be full,use:$use" |mail -s "disk warning" 88563128@qq.com
done

[root@rocky8 ~]# cp /dev/zero /boot/f1.img
cp: error writing '/boot/f1.img': No space left on device
[root@rocky8 ~]# bash disk_check.sh

范例:

[root@rocky8 ~]# vim while_read_check_ddos.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-20
#FileName:      while_read_check_ddos.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
lastb | sed -rn '/ssh:/s@.* ([0-9.]{1,3}{3}[0-9]{1,3}) .*@\1@p'|sort |uniq -c |while read count ip ;do
    if [ $count -gt 3 ];then
        iptables -A INPUT -s $ip -j REJECT
    fi
done

范例:查看/sbin/nologin的shell类型的用户名和UID

[root@rocky8 ~]# vim while_read_passwd.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-20
#FileName:      while_read_passwd.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************

while read line ;do
    if [[ "$line" =~ /sbin/nologin$ ]] ;then
        echo $line | cut -d: -f1,3
    fi
done < /etc/passwd

[root@rocky8 ~]# bash while_read_passwd.sh 
bin:1
daemon:2
adm:3
lp:4
mail:8
operator:11
games:12
ftp:14
nobody:65534
dbus:81
systemd-coredump:999
systemd-resolve:193
tss:59
polkitd:998
unbound:997
sssd:996
sshd:74


这篇关于36.第六章 Shell脚本编程高级进阶(一)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程