Linux练习(Part6.6:shell脚本编程)


练习:用while实现

1、编写脚本,求100以内所有正奇数之和

[09:16:38 root@Centos8 /data/Mage]#cat while_sum100.sh
#!/bin/bash
#
#********************************************************************
#Author:                    chenchunyu
#QQ:                        2504039287
#Date:                      2021-01-27
#FileName:                 while_sum100.sh
#URL:                       https://www.cnblogs.com/chenchunyuBEyOND/
#Description:          The test script
#Copyright (C):         2021 All rights reserved
#********************************************************************
#求100以内所有正奇数之和
SUM=0
i=1
while [ $i -le 100 ];do
    SUM=$[${SUM}+${i}]
    i=$[$i+2]
done
echo "100以内所有正奇数之和等于: $SUM"
[09:16:50 root@Centos8 /data/Mage]#bash while_sum100.sh
100以内所有正奇数之和等于: 2500

2、编写脚本,提示请输入网络地址,如192.168.0.0,判断输入的网段中主机在线状态,并统计在线和离线主机各多少

[10:44:39 root@Centos8 /data/Mage]#cat while_ping.sh
#!/bin/bash
#
#********************************************************************
#Author:                    chenchunyu
#QQ:                        2504039287
#Date:                      2021-01-27
#FileName:                 while_ping.sh
#URL:                       https://www.cnblogs.com/chenchunyuBEyOND/
#Description:          The test script
#Copyright (C):         2021 All rights reserved
#********************************************************************
#提示请输入网络地址,如192.168.0.0,判断输入的网段中主机在线状态,并统计在线和离线主机各多少
read -p "Enter NET(eg:192.168.0.0): " NET
N=`echo $NET|sed -nr 's/(.*\..*\.).*\..*/\1/p'`
#N=`echo $NET|cut -d. -f1-2`
i=0
online=0
offline=0
while [ $i -le 255 ];do
    j=1
    while [ $j -le 254 ];do
        if ping -c2 -i0.2 -W1 $N$i.$j &> /dev/null;then
            echo "$N$i.$j is online"
            let online++
        else
            echo "$N$i.$j is offline"
            let offline++
        fi
        let j++
    done
    let i++
done
echo "在线:$online"
echo "不在线:$offline"
#{
#}&
#wait
#效率低,有什么方法能并行执行并统计?待验证!
[10:44:49 root@Centos8 /data/Mage]#bash while_ping.sh
Enter NET(eg:192.168.0.0): 100.0.0.0
100.0.0.1 is online
100.0.0.2 is online
100.0.0.3 is offline
100.0.0.4 is offline
100.0.0.5 is offline
100.0.0.6 is offline
100.0.0.7 is offline
100.0.0.8 is offline

3、编写脚本,打印九九乘法表

[10:53:19 root@Centos8 /data/Mage]#cat 99.sh
#!/bin/bash
#
#********************************************************************
#Author:                    chenchunyu
#QQ:                        2504039287
#Date:                      2021-01-25
#FileName:                 99.sh
#URL:                       https://www.cnblogs.com/chenchunyuBEyOND/
#Description:          The test script
#Copyright (C):         2021 All rights reserved
#********************************************************************
#九九乘法表
#for语法1
#for i in {1..9};do
#    for j in `seq $i`;do
#        echo -e "${j}x${i}=$[i*j]\t\c"
#    done
#    echo
#done

#for语法2
#for ((i=1;i<10;i++));do
#    for((j=1;j<=i;j++));do
#        echo -e "${j}x${i}=$((i*j))\t\c"
#    done
#    echo
#done
#while
i=1
while [ $i -lt 10 ];do
    j=1
    while [ $j -le $i ];do
        echo -e "${j}x${i}=$((i*j))\t\c"
        let j++
    done
    echo
    let i++
done
[10:53:22 root@Centos8 /data/Mage]#bash 99.sh
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

4、编写脚本,利用变量RANDOM生成10个随机数字,输出这个10数字,并显示其中的最大值和最小值

[11:20:42 root@Centos8 /data/Mage]#cat while_RANDOM10.sh
#!/bin/bash
#
#********************************************************************
#Author:                    chenchunyu
#QQ:                        2504039287
#Date:                      2021-01-27
#FileName:                 while_RANDOM10.sh
#URL:                       https://www.cnblogs.com/chenchunyuBEyOND/
#Description:          The test script
#Copyright (C):         2021 All rights reserved
#********************************************************************
#利用变量RANDOM生成10个随机数字,输出这10个数字,并显示其中的最大值和最小值
declare -i a=0
max=0
min=$RANDOM
while [ $a -le 9 ];do
num=$RANDOM
echo $num
    if [ $num -le $max ];then
        if [ $num -le $min ];then
            min=$num
        fi
    else
        max=$num
    fi
    let a++
done
echo "the min number is $min,the max number is $max"
[11:20:53 root@Centos8 /data/Mage]#bash while_RANDOM10.sh
5357
29779
3233
21268
19353
13739
586
26642
12419
8750
the min number is 586,the max number is 29779

5、编写脚本,实现打印国际象棋棋盘

6、后续六个字符串:efbaf275cd、4be9c40b8b、44b2395c46、f8c8873ce0、b902c16c8b、ad865d2f63是通过对随机数变量RANDOM随机执行命令: echo $RANDOM|md5sum|cut -c1-10 后的结果,请破解这些字符串对应的RANDOM值

posted @ 2021-01-27 09:13  加油啊坚持啊搞钱啊  阅读(175)  评论(0)    收藏  举报