(十)while和until循环

时间:2022-03-20 20:30:51

(1)while循环

语法:当条件测试成立(真),执行循环体
while 条件测试
do
循环体
done

1)while批量创建用户1

从user.txt读取到的行数据赋予给变量user值

#!/bin/bash
while read user 
do
        id $user &>/dev/null
        if [ $? -eq 0 ];then
                echo "user $user is already exists!"
        else
                useradd $user
                if [ $? -eq 0 ];then
                        echo "user $user is created!"
                fi
        fi
done <user.txt
cat user.txt
jack01
jack02
jack03
jack04

2)while批量创建用户2

while默认空格作为分割,非常适合处理文件

#!/bin/bash
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 $user is already exists!"
        else
                useradd $user
                echo $pass | passwd --stdin $user &>/dev/null
                if [ $? -eq 0 ];then
                        echo "user $user is created!"
                fi
        fi
        }&
done <$1
wait
echo "all ok....."
# cat user2.txt 
jack001 123
jack002 123
jack03  123
jack04  123

3)while对有空行文件的处理方式

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

(2)until循环

语法:当条件测试成立(假),执行循环体
until 条件测试
do
循环体
done

#!/bin/bash
ip=114.114.114.114
until ping -c1 -W1 $ip &>/dev/null
do
        sleep 1
done
echo "$ip is up"

(3)for,while,until 循环ping比较

#!/bin/bash
for i in {2..209}
do
        {
        ip=192.168.111.$i
        ping -c1 -W1  $ip &>/dev/null
        if [ $? -eq 0 ];then
                echo "$ip is up"
        fi
        }&
done
wait
echo "all done...."
#!/bin/bash
i=2
while [ $i -le 254 ]
do
        {
        ip=192.168.111.$i
        ping -c1 -W1  $ip &>/dev/null
        if [ $? -eq 0 ];then
                echo "$ip is up"
        fi
        }&
        let i++
done
wait
echo "all done...."
#!/bin/bash
i=2
until [ $i -gt 254 ]
do
        {
        ip=192.168.111.$i
        ping -c1 -W1  $ip &>/dev/null
        if [ $? -eq 0 ];then
                echo "$ip is up"
        fi
        }&
        let i++
done
wait
echo "all done...."

(4)for,while,until 循环求1到100的和

#!/bin/bash
for i in {1..100}
do
        #let sum=$sum+$i
        let sum+=$i
done
echo "sum:$sum"
#!/bin/bash
i=1
sum=0
while [ $i -le 100 ]
do
        #let sum=$sum+$i
        let sum+=$i
        let i++
done
echo "sum:$sum"
#!/bin/bash
i=1
until [ $i -gt 100 ]
do
        #let sum=$sum+$i
        let sum+=$i
        let i++
done
echo "sum:$sum"

总结:循环次数固定建议使用for循环,循环文件建议使用while循环,循环次数不固定建议使用while和until循环