美文网首页
note_13.6_shell循环

note_13.6_shell循环

作者: 人間失格_430b | 来源:发表于2019-03-06 21:59 被阅读0次

循环执行:for, while, until

  • for循环格式:
    for VARAIBLE in LIST; do
    循环体
    done

  • while循环:
    while CONDITION; do
    循环体
    done

  • until循环:
    until CONDITION; do
    循环体
    done

    进入条件:CONDITION测试为”假“
    退出条件:CONDITION测试为”真“


练习:

分别使用for, while, until实现
1、分别求100以内所有偶数之和,100以内所奇数之和;

[root@localhost scripts]# ./e13_6_1.sh 
for loop : oddsum 2500 , evensum 2550
while loop : oddsum 2500 , evensum 2550
until loop : oddsum 2500 , evensum 2550
[root@localhost scripts]# cat e13_6_1.sh 
#!/bin/bash
declare -i sum4for=0 sum4while=0 sum4until=0
for i in {1..100};do
    if [ $[$i%2] -eq 1 ];then
        let sum4for+=$i
    fi
done
echo "for loop : oddsum $sum4for , evensum $[5050-$sum4for]"
let i=1
while [ $i -lt 100 ];do
    let sum4while+=i
    let i+=2
done
echo "while loop : oddsum $sum4while , evensum $[5050-$sum4while]"
let i=1
until [ $i -gt 100 ];do
    let sum4until+=i
    let i+=2
done
echo "until loop : oddsum $sum4until , evensum $[5050-$sum4until]"

2、创建10个用户,user101-user110;密码同用户名;

[root@localhost scripts]# bash ./e13_6_2.sh 
id: user101: no such user
id: user102: no such user
id: user103: no such user
id: user104: no such user
id: user105: no such user
id: user106: no such user
id: user107: no such user
id: user108: no such user
id: user109: no such user
id: user110: no such user
uid=4007(user101) gid=4007(user101) groups=4007(user101)
uid=4008(user102) gid=4008(user102) groups=4008(user102)
uid=4009(user103) gid=4009(user103) groups=4009(user103)
uid=4010(user104) gid=4010(user104) groups=4010(user104)
uid=4011(user105) gid=4011(user105) groups=4011(user105)
uid=4012(user106) gid=4012(user106) groups=4012(user106)
uid=4013(user107) gid=4013(user107) groups=4013(user107)
uid=4014(user108) gid=4014(user108) groups=4014(user108)
uid=4015(user109) gid=4015(user109) groups=4015(user109)
uid=4016(user110) gid=4016(user110) groups=4016(user110)
uid=4007(user101) gid=4007(user101) groups=4007(user101)
uid=4008(user102) gid=4008(user102) groups=4008(user102)
uid=4009(user103) gid=4009(user103) groups=4009(user103)
uid=4010(user104) gid=4010(user104) groups=4010(user104)
uid=4011(user105) gid=4011(user105) groups=4011(user105)
uid=4012(user106) gid=4012(user106) groups=4012(user106)
uid=4013(user107) gid=4013(user107) groups=4013(user107)
uid=4014(user108) gid=4014(user108) groups=4014(user108)
uid=4015(user109) gid=4015(user109) groups=4015(user109)
uid=4016(user110) gid=4016(user110) groups=4016(user110)
[root@localhost scripts]# cat ./e13_6_2.sh
#!/bin/bash
declare -i i=0
for i in {101..110};do
    id user$i || useradd -p user$i user$i
done
i=100
while [ $i -lt 110 ];do
    let i+=1
    id user$i || useradd -p user$i user$i
done
i=100
until [ $i -ge 110 ];do
    let i+=1
    id user$i || useradd -p user$i user$i
done
[root@localhost scripts]# bash ./e13_userdel.sh 
[root@localhost scripts]# cat ./e13_userdel.sh
#!/bin/bash
for i in `grep -o 'user[0-9]\+' /etc/passwd | uniq`;do
    userdel -r $i
done

3、打印九九乘法表;

[root@localhost scripts]# bash e13_6_3.sh
for loop print:
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  

while loop print:
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  

until loop print:
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  
[root@localhost scripts]# cat e13_6_3.sh
#!/bin/bash
declare -i r=0 l=0
echo "for loop print:"
for i in {1..9};do
    for j in `seq 1 $i`;do
        echo -en "${j}x${i}=$[$i*$j]\t"
    done
    echo
done
echo -e "\nwhile loop print:"
let r=1
let l=1
while [ $r -lt 10 ];do
    while [ $l -le $r ];do
        echo -ne "${l}x${r}=$[$r*$l]\t"
        let l+=1
    done
    echo
    let l=1
    let r+=1
done
let r=1
let l=1
echo -e "\nuntil loop print:"
until [ $r -ge 10 ];do
    until [ $l -gt $r ];do
        echo -ne "${l}x${r}=$[$r*$l]\t"
        let l+=1
    done
    echo
    let l=1
    let r+=1
done

4、打印逆序的九九乘法表;

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

while loop print:
1x9=9   2x9=18  3x9=27  4x9=36  5x9=45  6x9=54  7x9=63  8x9=72  9x9=81  
1x8=8   2x8=16  3x8=24  4x8=32  5x8=40  6x8=48  7x8=56  8x8=64  
1x7=7   2x7=14  3x7=21  4x7=28  5x7=35  6x7=42  7x7=49  
1x6=6   2x6=12  3x6=18  4x6=24  5x6=30  6x6=36  
1x5=5   2x5=10  3x5=15  4x5=20  5x5=25  
1x4=4   2x4=8   3x4=12  4x4=16  
1x3=3   2x3=6   3x3=9   
1x2=2   2x2=4   
1x1=1   

until loop print:
1x9=9   2x9=18  3x9=27  4x9=36  5x9=45  6x9=54  7x9=63  8x9=72  9x9=81  
1x8=8   2x8=16  3x8=24  4x8=32  5x8=40  6x8=48  7x8=56  8x8=64  
1x7=7   2x7=14  3x7=21  4x7=28  5x7=35  6x7=42  7x7=49  
1x6=6   2x6=12  3x6=18  4x6=24  5x6=30  6x6=36  
1x5=5   2x5=10  3x5=15  4x5=20  5x5=25  
1x4=4   2x4=8   3x4=12  4x4=16  
1x3=3   2x3=6   3x3=9   
1x2=2   2x2=4   
1x1=1   
[root@localhost scripts]# cat e13_6_4.sh
#!/bin/bash
declare -i r=0 l=0
echo "for loop print:"
for i in `seq 9 -1 1`;do
    for j in `seq 1 $i`;do
        echo -en "${j}x${i}=$[$i*$j]\t"
    done
    echo
done
echo -e "\nwhile loop print:"
let r=9
let l=1
while [ $r -ge 1 ];do
    while [ $l -le $r ];do
        echo -ne "${l}x${r}=$[$r*$l]\t"
        let l+=1
    done
    echo
    let l=1
    let r-=1
done
let r=9
let l=1
echo -e "\nuntil loop print:"
until [ $r -lt 1 ];do
    until [ $l -gt $r ];do
        echo -ne "${l}x${r}=$[$r*$l]\t"
        let l+=1
    done
    echo
    let l=1
    let r-=1
done

相关文章

网友评论

      本文标题:note_13.6_shell循环

      本文链接:https://www.haomeiwen.com/subject/gspxpqtx.html