shell 循环学习练习

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

目录

一嵌套循环实现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

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: shell