shell 循环学习练习

目录

一,嵌套循环实现9*9乘法表

二,判定一个成绩:

三,循环创建用户:用户名为user01-user20


一,嵌套循环实现9*9乘法表

       (for和while都可以)

       选做:格式对齐,以及使用单层循环完成9*9乘法表

tips:

-n 不输出尾随换行符

-e 启用反斜杠转义

 -t  水平制表符

 1,使用命令 vim 99table_script.sh打开脚本文件

[root@wangjingjing ~]# vim 99table_script.sh

2,按照要求编写脚本文件

for ((a=1;a<=9;a++))

do

   for ((b=1;b<=a;b++))

   do

        echo -ne "$a*$b=$(($a*$b))\t"

   done

   echo -e "\n"

done

3,使用命令  bash 99table_script.sh 执行脚本文件,结果如下:

[root@wangjingjing ~]# bash 99table_script.sh

1*1=1        

2*1=2        2*2=4        

3*1=3        3*2=6        3*3=9        

4*1=4        4*2=8        4*3=12        4*4=16        

5*1=5        5*2=10        5*3=15        5*4=20        5*5=25        

6*1=6        6*2=12        6*3=18        6*4=24        6*5=30        6*6=36        

7*1=7        7*2=14        7*3=21        7*4=28        7*5=35        7*6=42        7*7=49        

8*1=8        8*2=16        8*3=24        8*4=32        8*5=40        8*6=48        8*7=56        8*8=64        

9*1=9        9*2=18        9*3=27        9*4=36        9*5=45        9*6=54        9*7=63        9*8=72        9*9=81        

二,判定一个成绩:

85-100 -》 A

70-84 -> B

60-69 -> C

0-59 ->D

程序的边界问题:>100, <0

必须输入的是数字:0-100

 1,使用命令 vim score_script2.sh打开脚本文件

[root@wangjingjing ~]# vim score_script2.sh

2,按照要求编写脚本文件

score=$1

if [ $score -gt 100 -o $score -lt 0 ]

then

    echo "wrong number"

elif [ $score -ge 85 ]

then

    echo "LEVEL A"

elif [ $score -ge 70 ]

then

    echo "LEVEL B"

elif [ $score -ge 60 ]

then

    echo "LEVEL C"

elif [ $score -ge 0  ]

then

    echo "LEVEL D"

fi

3,使用命令   bash score_script2.sh + 参数   执行脚本文件,结果如下:

[root@wangjingjing ~]# bash score_script2.sh 100

LEVEL A

[root@wangjingjing ~]# bash score_script2.sh 75

LEVEL B

[root@wangjingjing ~]# bash score_script2.sh 65

LEVEL C

[root@wangjingjing ~]# bash score_script2.sh 55

LEVEL D

[root@wangjingjing ~]# bash score_script2.sh 666

wrong number

[root@wangjingjing ~]# bash score_script2.sh -123

wrong number

三,循环创建用户:用户名为user01-user20

 1,使用命令  vim user_script2.sh 打开脚本文件

[root@wangjingjing ~]# vim user_script2.sh

2,按照要求编写脚本文件

for user in `seq -f "user%02g" 20`

do

    useradd $user

done

3,使用命令bash user_script2.sh加载文件

[root@wangjingjing ~]# bash user_script2.sh

4,测试结果如下所示:

[root@wangjingjing ~]# tail -20 /etc/passwd

user01:x:1001:1001::/home/user01:/bin/bash

user02:x:1002:1002::/home/user02:/bin/bash

user03:x:1003:1003::/home/user03:/bin/bash

user04:x:1004:1004::/home/user04:/bin/bash

user05:x:1005:1005::/home/user05:/bin/bash

user06:x:1006:1006::/home/user06:/bin/bash

user07:x:1007:1007::/home/user07:/bin/bash

user08:x:1008:1008::/home/user08:/bin/bash

user09:x:1009:1009::/home/user09:/bin/bash

user10:x:1010:1010::/home/user10:/bin/bash

user11:x:1011:1011::/home/user11:/bin/bash

user12:x:1012:1012::/home/user12:/bin/bash

user13:x:1013:1013::/home/user13:/bin/bash

user14:x:1014:1014::/home/user14:/bin/bash

user15:x:1015:1015::/home/user15:/bin/bash

user16:x:1016:1016::/home/user16:/bin/bash

user17:x:1017:1017::/home/user17:/bin/bash

user18:x:1018:1018::/home/user18:/bin/bash

user19:x:1019:1019::/home/user19:/bin/bash

user20:x:1020:1020::/home/user20:/bin/bash

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值