Shell script-while loop statement (including application cases)

While loop syntax structure

while 表达式
    do
        command...
    done

while  [ 1 -eq 1 ] 或者 (( 2 > 1 ))
  do
     command
     command
     ...
 done
 
 当条件测试成立(必须为真),返回值为0才能执行循环体

Applications

Calculate the sum of 1-100 even numbers

#!/bin/bash
#定义变量
sum=0
i=2
while [ $i -le 100 ]
do
    let sum=$sum+$i
    let i+=2
done
echo "1-100的偶数和为:$sum"

# for
#!/bin/bash
sum=0
for ((i=0;i<=100;i+=2))
do
    let sum=$sum+$i 
done
echo "1-100的偶数和为:$sum"

Synchronize system time

  • Write a script to synchronize the system time every 30 seconds, and the time synchronization server 192.168.188.188
  • If the synchronization fails, an email alarm will be sent, and an alarm will be sent every time it fails
  • If the synchronization is successful, it will also be notified by email, but it will only be notified once 100 times.
#!/bin/bash
# 该脚本用于时间同步
NTP=10.1.1.1
count=0
while true
do
    ntpdate $NTP &>/dev/null
    if [ $? -ne 0 ];then
        echo "system date failed" |mail -s "check system date"  root@localhost
    else
        let count++
        if [ $count -eq 100 ];then
        echo "systemc date success" |mail -s "check system date"  root@localhost && count=0
        fi
    fi
sleep 30
done

Right triangle

#!/bin/bash
read -p "请输入三角形的高:" h
i=1
while [ $i -le $h ]
do
        j=1 
        while [ $j -le $i ]
        do
                echo -n "*"
                let j++ 
        done
        echo
        let i++ 
done 

Isosceles triangle

#!/bin/bash
read -p "请输入三角形的高:" h
i=1
while [ $i -le $h ]
do
        a=$[ $h - $i ]
        j=1 
        while [ $j -le $a ]
        do
                echo -n " "
                let j++ 
        done

        b=$((2*$i - 1 ))
        k=1 
        while [ $k -le $b ]
        do
                echo -n "*"
                let k++ 
        done
        echo
        let i++
done

Inverted isosceles triangle

#!/bin/bash
read -p "请输入三角形的高:" h
i=1
while [ $i -le $h ]
do
        j=1 
        while [ $j -le $i ]
        do
                echo -n " "
                let j++ 
        done

        k=0 
        a=$(( 2*($h-$i) - 1 ))
        while [ $k -lt $a ]
        do
                echo -n "*"
                let k++ 
        done
        echo
        let i++
done

diamond

#!/bin/bash
# by stanZ
read -p "请输入菱形半径:" h
i=1
while [ $i -le $h ]
do
        a=$[ $h -$i ]
        j=1 
        while [ $j -le $a ]
        do
                echo -n " "
                let j++ 
        done

        b=$((2*$i - 1))
        k=1 
        while [ $k -le $b ]
        do
                echo -n "*"
                let k++ 
        done
        echo
        let i++
done

u=1
while [ $u -lt $h ]
do
        for((n=1;n<=$u;n++))
        do
                echo -n " "
        done

        l=$(( 2*($h-$u)-1 ))
        for((m=1;m<=$l;m++))
        do
                echo -n "*"
        done
        echo
        let u++
done


Create users in bulk

We will write the username and password that need to be created into the file and use while traversal to create

假设文件是ip.txt
maomao 123
zhuzhu 456
niuniu 789
#!/bin/bash
#while create user
#v1.0 by stanZ 2020-12-17
if [ $# -eq 0 ];then
        echo "usage:`basename $0` file"
        exit 10
fi

if [ ! -f $1 ];then
        echo "error file"
        exit 5
fi

while read line
do
        user=`echo $line |awk '{print $1}'`
        pass=`echo $line |awk '{print $2}'`
        id $user &>/dev/null
        if [ $? -eq 0 ];then    
                echo "$user already exists"
        else
                useradd $user
                echo "$pass" |passwd --stdin $user &>/dev/null
                if [ $? -eq 0 ];then
                        echo "$user is created."
                fi
        fi
        done < $1

Test network segment surviving host

If ping fails for 3 times, it is judged that the host is not alive

#!/bin/bash
while read ip
do
        fail_count=0
        #for i in `seq 3`
        for i in {
    
    1..3}
        do  
                ping -c1 -W1 $ip &>/dev/null
                if [ $? -eq 0 ];then
                        echo "$ip ping is ok."
                        break
                else
                        echo "$ip ping is failure:$i"
                        let fail_count++
                fi  
        done
        if [ $fail_count -eq 3 ];then
                echo "$ip ping is failure!!!"
        fi  
done <ip.txt

Guess you like

Origin blog.csdn.net/Cantevenl/article/details/115268788