Chapter 6 Advanced Shell script programming

4.2.4 while loop

Format:

while COMMANDS; do COMMANDS; done

while CONDITION; do
    Circulatory body
done

explain:

CONDITION: cycle control CONDITION; Make a judgment before entering the cycle; Judgment will be made again after each cycle; If the CONDITION is "true", a loop is executed; The loop is terminated until the CONDITION test status is "false". Therefore, the CONDTION should generally have a loop control variable; The value of this variable is constantly modified in the loop body

Entry CONDITION: CONDITION is true

Exit CONDITION: CONDITION is false

Infinite loop

while true; do
    Circulatory body
done

example:

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

example:

[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.
#: is the same as true 

example:

[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

example:

[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
#If the disk space is greater than 80%, the mailbox will receive mail

[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

Example: 99 multiplication table

[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 exercise: implement with while

1. Write a script to find the sum of all positive and odd numbers within 100
2. Write a script and prompt to enter the network address, such as 192.168.0.0. Judge the online status of the host in the entered network segment, and count the number of online and offline hosts
3. Write script and print 99 multiplication table
4. Write a script, use the variable RANDOM to generate 10 RANDOM numbers, output the 10 numbers, and display the maximum and minimum values
5. Write a script to print the chess board
6. The following six strings: efbaf275cd, 4be9c40b8b, 44b2395c46, f8c8873ce0, b902c16c8b and ad865d2f63 are the result of randomly executing the command echo $RANDOM|md5sum|cut -c1-10 on the RANDOM number variable RANDOM. Please crack the corresponding RANDOM values of these strings

4.2.6 until cycle

Format:

until COMMANDS; do COMMANDS; done

until CONDITION; do
    Circulatory body
done

explain:

Entry CONDITION: CONDITION is false

Exit CONDITION: CONDITION is true

Infinite loop

until false; do
    Circulatory body
Done

example:

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

example:

[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

example:

[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 ~]$ 
#Email when you log in

[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.
#Keep sending e-mail as long as you find your login

4.2.7 loop control statement continue

continue [N]: end the current cycle of layer N in advance and directly enter the next round of judgment; The innermost layer is the first layer

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

example:

[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 ends the loop of this layer

[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 ends the previous cycle

4.2.8 loop control statement break

break [N]: end the whole cycle of layer N in advance, and the innermost layer is layer 1

example:

[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 exit this layer loop

[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 exit the previous loop

example:

[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) Abalone
2) Man Han banquet
3) lobster
4) Bird's nest
5) King crab
6) sign out
EOF
    echo -e "\033[0m"

    read -p "Take your order, please(1-6): " MENU
    case $MENU in
    1|4)
        $COLOR'Vegetable price: $10'$END
        let sum+=10
        ;;
    3|5)
        $COLOR'Vegetable price: $20'$END
        let sum+=20
        ;;
    2)
        $COLOR'Vegetable price: $1000'$END
        let sum+=1000
        ;;
    6)
        $COLOR2"What's the total price of your order:\$$sum"$END
        break
        ;;
    *)
        echo "click the wrong place,There is no such dish"
        ;;
    esac
    $COLOR2"What's the total price of your order:\$$sum"$END
done

[root@rocky8 ~]# bash menu.sh 
1) Abalone
2) Man Han banquet
3) lobster
4) Bird's nest
5) King crab
6) sign out

Take your order, please(1-6): 1
 Vegetable price: $10
 What's the total price of your order:$10
1) Abalone
2) Man Han banquet
3) lobster
4) Bird's nest
5) King crab
6) sign out

Take your order, please(1-6): 5
 Vegetable price: $20
 What's the total price of your order:$30
1) Abalone
2) Man Han banquet
3) lobster
4) Bird's nest
5) King crab
6) sign out

Take your order, please(1-6): 6
 What's the total price of your order:$30

example:

[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 "Enter 0-9 Number between: " INPUT ;do
    if [ $INPUT -eq $NUM ];then
        echo "Congratulations, you guessed right!"
        break
    elif [ $INPUT -gt $NUM ];then
        echo "The number is too big,Guess again!"
    else
        echo "The number is too small,Guess again!"
    fi
done

[root@rocky8 ~]# bash guess.sh 
Enter 0-9 Number between: 8
 The number is too big,Guess again!
Enter 0-9 Number between: 7
 The number is too big,Guess again!
Enter 0-9 Number between: 5
 Congratulations, you guessed right!

4.2.9 cycle control shift command

shift [n] is used to move the parameter list left a specified number of times. The default is to move left once.
Once the parameter list is moved, the leftmost parameter is deleted from the list. When the while loop traverses the position parameter list, shift is often used

example:

[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 practice

1. Obtain the information of the logged in user on the system every 3 seconds; If the user hacker is found logged in, the login time and host are recorded in the log / var/log/login.log, and the script is exited
2. Randomly generate numbers within 10 to realize the guessing game. If the prompt is large or small, exit if it is equal
3. Use the file name as the parameter to count the total number of rows of all parameter files
4. Use more than two numbers as parameters to display the maximum and minimum values

4.2.11 special usage of while read

The special use of the while loop to traverse each line of file or text

Format:

while read line; do
    Circulatory body
done < /PATH/FROM/SOMEFILE

Note: read each line in the / PATH/FROM/SOMEFILE file in turn, and assign the line to the variable line

example:

[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

example:

[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

example:

[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

Example: view the user name and UID of the shell type of / sbin/nologin

[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

Keywords: Operation & Maintenance bash architecture DevOps Cloud Native

Added by easethan on Sun, 07 Nov 2021 01:01:19 +0200