练习7

2020-04-05  本文已影响0人  Stone_説

用while循环完成以下练习
1.编写脚本,求100以内所有正奇数之和

[root@centos7 ~]# vim test.sh
#!/bin/bash
let i=1
let sum=0
while [ $i -lt 100 ];do
    let sum+=i
    let i+=2
done
echo sum is $sum
[root@centos7 ~]# ./test.sh 
sum is 2500

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

[root@centos7 ~]# vim test.sh
#!/bin/bash
let i=1
let up_num=0
let down_num=0
read -p "please input an ip:" IP
net=`echo $IP|cut -d'.' -f1-3`
while [ ${i} -le 254 ];do
        ping -c1 -w1 $net.$i &> /dev/null
        if [ $? -eq 0 ];then
                echo "${net}.${i} is up"
                up_num=$(($up_num+1))
        else
                echo "${net}.${i} is down"
                down_num=$(($down_num+1))
        fi
        i=$(($i+1))
done
echo "total ${up_num} comp are up"
echo "total ${down_num} comp are down"
[root@centos7 ~]# ./test.sh 
please input an ip:192.168.43.2
192.168.43.1 is up
192.168.43.2 is up
192.168.43.3 is down
......
192.168.43.140 is up
192.168.43.141 is up
192.168.43.142 is down
192.168.43.143 is up
...
192.168.43.153 is down
192.168.43.154 is down
192.168.43.155 is up
...
192.168.43.254 is down
total 6 comp are up
total 248 comp are down

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

[root@centos7 ~]# vim test2.sh
#!/bin/bash
let i=1
while [ ${i} -le 9 ];do
    let j=1
    while [ ${j} -le ${i} ];do
        echo -e "${i}x${j}=$[j*i]\t\c"
        let j+=1
    done    
    let i+=1    
    echo -e "\t"
done
[root@centos7 ~]# ./test2.sh 
1x1=1       
2x1=2   2x2=4       
3x1=3   3x2=6   3x3=9       
4x1=4   4x2=8   4x3=12  4x4=16      
5x1=5   5x2=10  5x3=15  5x4=20  5x5=25      
6x1=6   6x2=12  6x3=18  6x4=24  6x5=30  6x6=36      
7x1=7   7x2=14  7x3=21  7x4=28  7x5=35  7x6=42  7x7=49      
8x1=8   8x2=16  8x3=24  8x4=32  8x5=40  8x6=48  8x7=56  8x8=64      
9x1=9   9x2=18  9x3=27  9x4=36  9x5=45  9x6=54  9x7=63  9x8=72  9x9=81

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

[root@centos7 ~]# vim test3.sh 
#!/bin/bash
let i=1
let j=3
let a_text=0

while [ ${i} -le 10 ];do
    let r_num${i}=${RANDOM}
    echo r_num${i} is $[r_num${i}]
    let i+=1
done

[ ${r_num1} -gt ${r_num2} ] && let max_num=$r_num1 && let min_num=$r_num2
[ $? -ne 0 ] && max_num=$r_num2 && let min_num=$r_num1

while [ ${j} -le 10 ];do
    [ $((r_num${j})) -gt $max_num ] && max_num=$((r_num${j})) && let j+=1 && continue
    [ $? -ne 0 ] && [ $((r_num${j})) -lt $min_num ] && min_num=$((r_num${j})) && let j+=1 && continue
    let j+=1
done

echo max is $max_num
echo min is $min_num

[root@centos7 ~]# ./test3.sh 
r_num1 is 30319
r_num2 is 366
r_num3 is 2971
r_num4 is 1089
r_num5 is 28940
r_num6 is 616
r_num7 is 3808
r_num8 is 13939
r_num9 is 17461
r_num10 is 12004
max is 30319
min is 366

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

[root@centos7 ~]# vim chess.sh
#!/bin/bash
for i in {1..8};do
        for j in {1..8};do
                sum=$[i+j]
                if [ $[sum%2] -eq 0 ];then
                        echo -ne "\033[43m \033[0m"
                else
                echo -ne "\033[45m \033[0m"
                fi
        done
        echo
done

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

[root@node1 ~]# vi test4.sh
[root@node1 ~]# cat test4.sh
#!/bin/bash
read -p "please enter string:" VAR
for i in {0..32767};do
    var=`echo $i|md5sum|cut -c1-10`
    if [ $VAR = $var ];then
        echo "$i-->$var" 
        let i+=1
        break
    fi
    let i+=1
done
[root@node1 ~]# ./test4.sh
please enter string:4be9c40b8b
12000-->4be9c40b8b
上一篇下一篇

猜你喜欢

热点阅读