bash 循环语句while、until练习

bash 循环语句while、until

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

(0)
wangnannanwangnannan
上一篇 2016-08-19 08:36
下一篇 2016-08-19 08:36

相关推荐

  • 基于keepalived实现高可用nginx服务

    基于keepalived实现高可用nginx服务 环境及配置前提说明 主机1,ip:192.168.25.140 运行web服务 主机2,ip:192.168.25.141 运行web服务 主机3,ip:192.168.25.138 运行nginx服务和keepalived服务 主机4,ip:192.168.25.139 运行nginx服务和keepaliv…

    Linux干货 2016-11-07
  • linux文件查找工具 — find

    简述:   linux中find命令是一种强大的实时查找工具,它通过用户给出的路径,在该路径下的文件系统中进行文件查找。因此在遍历一个较大的文件系统时会比较花费时间,而且find命令占用资源也是比较大的,所以它的工作特点是,查找的速度略慢,但是可以实现精确查找和实时查找。由于用户权限的问题,可能只搜索用户具备读取和执行权限的目录。由于fi…

    Linux干货 2016-08-18
  • 常见文件管理命令及bash特性

    cat:查看文件文本内容  -n:显示行号  [root@study ~]# cat -n /etc/passwd  1  root:x:0:0:root:/root:/bin/bash  2  bin:x:1:1:bin:/bin:/sb…

    系统运维 2016-11-06
  • web服务 初步

    Web Service初步 一、引言 Web服务也是一个C/S架构,服务器端就是一个进程,客户端是一个浏览器。我们打开浏览器的时候,都会看到hppt,https的字样,然后才是输入网址,这两个协议是web服务的应用层协议,用来实现某些具体应用的。像https,前面学习openssl的时候也接触过,https=http+ssl。 web的传输层协议用到了tcp…

    Linux干货 2016-12-09
  • 超全超详细的HTTP状态码大全

    本部分余下的内容会详细地介绍 HTTP 1.1中的状态码。这些状态码被分为五大类:  100-199 用于指定客户端应相应的某些动作。 200-299 用于表示请求成功。 300-399 用于已经移动的文件并且常被包含在定位头信息中指定新的地址信息。 400-499 用于指出客户端的错误。 500-599 用…

    Linux干货 2015-03-20
  • N21_第x周_Storm_01_单机实践篇

       这2周没有按马哥安排的课程走,因公司需要,大家一直在试尝大数据这块。作业不能不做,也不知道马哥哪周的作业会有storm,只好先将这段时间的实验慢慢记录下来(其它flume、kafka、spark等本周会慢慢补充),等知道具体的作业题目后,再完善。 实验目的   了解storm的原理,并用storm单机版实验加深理解,为后面…

    Linux干货 2016-09-15