while练习题
1、求100以内所有正整数之和 2、通过ping命令探测172.16.250.1 - 254范围内的所有主机的在线状态,统计在线主机和离线主机各多少。 3、打印九九乘法表 4、利用变量RANDOM生成10个随机数字,输出这个10数字,并显示其中的最大者和最小者 5、打印国际象棋棋盘
答案
答案一 [root@centos7 bin]# [root@centos7 bin]# cat 100sunwhile.sh #!/bin/bash declare -x i i=1 while [ $i -le 100 ];do let sum+=$i let i++ done echo "1~100sum is :$sum" [root@centos7 bin]# 答案二 [root@centos7 bin]# cat hostpingwhile.sh #!/bin/bash declare -i i=0 declare -i j=0 ip=10.1.252. declare -i ip2=218 while [ $ip2 -le 254 ];do if ping -c1 -w1 $ip$ip2 &>/dev/null;then let i++ echo "$ip$ip2 host is online" else let j++ echo "$ip$ip2 host is not online" fi let ip2++ done echo "the number of online host is :$i " echo "the number of no online host is :$j " [root@centos7 bin]# 答案三 [root@centos7 bin]# cat chengfabiaowhile.sh #!/bin/bash declare -i i=1 while [ $i -le 9 ] ;do declare -i j=1 while [ $j -le $i ];do echo -e " $j*$i=$[ $i*$j ]\c" let j++ done let i++ echo -e "\n" done [root@centos7 bin]# 答案四 [root@centos7 bin]# cat Random.sh #!/bin/bash declare -i i=1 while [ $i -le 10 ];do let random=$RANDOM%80 echo "the $i shu is :$random" if [ $i -eq 1 ] then max=$random min=$random elif [[ $random -gt $max ]] then max=$random elif [[ $random -lt $min ]] then min=$random fi let i+=1 done echo "the max values is:$max" echo "the min values is:$min" [root@centos7 bin]# 答案5 [root@centos7 bin]# cat xiangqi.sh #!/bin/bash for n in `seq 1 8`;do for m in `seq 1 4`;do case $n in 1|3|5|7) echo -e "\033[41m \033[0m\c" echo -e "\033[47m \033[0m\c" ;; *) echo -e "\033[47m \033[0m\c" echo -e "\033[41m \033[0m\c" ;; esac done echo "" done [root@centos7 bin]# [root@centos7 bin]# cat xiangqi2.sh #!/bin/bash for i in {1..8};do for j in {1..8};do if [ $[$i%2] -eq 0 ];then echo -ne "\033[41m \033[m" else echo -ne "\033[42m \033[m" fi let i++ done echo done echo "$j" [root@centos7 bin]#
while特殊用法练习
1、扫描/etc/passwd文件每一行,如发现注释字段为空,则填充用户名和单位电话为62985600,并提示该用户的GECOS(注释)信息修改成功。 [root@centos7 until]# cat whileReadP.sh #!/bin/bash while read passwd ;do content=`echo $passwd |cut -d : -f5` username=`echo $passwd |cut -d: -f1` if [[ -z $content ]];then chfn -f "$username" $username &> /dev/null chfn -p "10086" $username &>/dev/mull fi done < /etc/passwd [root@centos7 until]# 2、将上题的添加清空 [root@centos7 until]# cat whileReadP.sh #!/bin/bash while read passwd ;do content=`echo $passwd |cut -d : -f5` username=`echo $passwd |cut -d: -f1` if [[ -n $content ]];then chfn -f "" $username &> /dev/null chfn -p "" $username &>/dev/mull fi done < /etc/passwd [root@centos7 until]#
until 循环
until CONDITION; do 循环体 done 进入条件: CONDITION 为false 退出条件: CONDITION 为true
until练习题
1、每隔3秒钟到系统上获取已经登录的用户的信息;如果发现用户hacker登录,则将登录时间和主机记录于日志/var/log/login.log中,并提示该用户退出系统。
2、随机生成10以内的数字,实现猜字游戏,提示比较大或小,相等则退出
3、编写脚本,求100以内所有正整数之和
4、编写脚本,通过ping命令探测172.16.250.1-254范围内的所有主机的在线状态,统计在线主机和离线主机各多少。
5、编写脚本,打印九九乘法表
6、编写脚本,利用变量RANDOM生成10个随机数字,输出这个10数字,并显示其中的最大者和最小者
7、编写脚本,实现打印国际象棋棋盘
8、用*打印等腰三角形
答案:
答案1 [root@centos7 until]# cat userlogininfo.sh #!/bin/bash #name:wangnannan #time:2010816 #description:checking the user longin information echo "check start" until false;do sleep 3 if who |grep hacker &>/dev/null;then who |tr -s " " |cut -d " " -f1,3,4 >> /var/log/login.log echo -e "find hackernlogin and /var/log/login.log have record ." echo "please hacker loginout " |write hacker else echo "not find hacker" fi done [root@centos7 until]# 答案二、 [root@centos7 until]# cat guess.sh #!/bin/bash #name:wangnannan #description:guess the Random let random=$RANDOM%11 until false ;do read -p "please you guess the Random[1-10] :" num if let $num+0 ;then if [ $num -gt $random ];then echo "you input greater then random" elif [ $num -lt $random ];then echo "you input lesser then random" elif [ $num -eq $random ];then echo "you guess ok" break fi else echo "plese input a inter" fi done [root@centos7 until]# 答案三 [root@centos7 until]# [root@centos7 until]# cat 100sum.sh #!/bin/bash #name:wangnannan #description:caculate 1-100 sum declare -i i=1 until [ $i -gt 100 ] ;do let sum+=i let i++ done echo "sum is :$sum" [root@centos7 until]# 答案四 [root@centos7 until]# cat hostping.sh #!/bin/bash declare -i i=0 declare -i j=0 read -p "please the ip :" ip if echo $ip |egrep -q "^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]).){3}([0-9]|[1-9][0-9]|1[0-9][0-9]| 2[0-4][0-9]|25[0-5])$";then ipv=`echo "$ip" | cut -d . -f 1,2,3` declare -i ip2=1 until [ $ip2 -gt 5 ];do if ping -c1 -w1 $ipv.$ip2 &>/dev/null;then let i++ echo "$ipv.$ip2 host is online" else let j++ echo "$ipv.$ip2 host is not online" fi let ip2++ done echo "the number of online host is :$i " echo "the number of no online host is :$j " else echo "input ip error" fi [root@centos7 until]# 答案五 [root@centos7 until]# cat chengfabiao #!/bin/bash #name:wangnannan #description:output chengfabiao declare -i i=1 until [ $i -gt 9 ];do declare -i j=1 until [ $j -gt $i ];do echo -ne "$j*$i=$[i*j]\t" let j++ done echo 换行输出 let i++ done [root@centos7 until]# 答案六 [root@centos7 until]# cat random.sh #!/bin/bash #name:wangnannan #description:generate ten random numbers and find the maximum and minimum values i=9 max=$[$RANDOM%10] min=$max echo "the random is:$max" until [ $i -le 0 ];do random=$[$RANDOM%10] echo "the random is:$random" let i-- if [ $random -ge $max ];then max=$random elif [ $random -lt $min ];then min=$random fi done echo "the maximum is:$max" echo "the minimum is:$min" [root@centos7 until]# 答案七: [root@centos7 until]# cat chessBoard.sh #!/bin/bash #name:wangnannan #description:output chess board i=1 until [ $i -gt 9 ];do j=1 until [ $j -gt 9 ];do m=$[(i+j)%2] if [ $m -eq 0 ];then echo -ne "\033[47m \033[0m" elif [ $m -eq 1 ];then echo -ne "\033[41m \033[0m" fi let j++ done let i++ echo done [root@centos7 until]# 答案八 正三角 [root@centos7 until]# [root@centos7 until]# cat iT.sh #!/bin/bash #name:wangnannan #description:output a isosceles triangle ues * read -p "please input a number n, I,ll output n hang isosceles triangle:" n until [[ $n =~ ^[[:digit:]]+$ ]];do echo -e "\033[32myour input is not a interger\nplesea chongxin input\033[m" read -p "please input a number n, I,ll output n hang isosceles triangle:" n done i=0 until [ $i -ge $n ];do j=0 x=0 until [ $x -ge $[n-(i+1)] ];do echo -n " " let x++ done until [ $j -ge $[2*i+1] ];do echo -ne "\033[31m*\033[m" let j++ done let i++ echo done [root@centos7 until]# 倒三角 [root@centos7 until]# cat riT.sh #!/bin/bash #name:wangnannan #description:oupput a triangle read -p "plese input a n:" n i=0 until [ $i -ge $n ];do h=0 until [ $h -ge $i ];do echo -n " " let h++ done j=0 until [ $j -ge $[2*(n-i)-1] ];do echo -ne "\033[32m*\033[m" let j++ done let i++ echo done [root@centos7 until]#
原创文章,作者:wangnannan,如若转载,请注明出处:http://www.178linux.com/36989