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.www58058.com/36989

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

相關推薦

  • 8月5號 練習+作業

    1,找出ifconfig 命令結果中本機的所有IPv4 地址 [root@localhost ~]# ifconfig |tr -cs '[:digit:].' '\n' |sort -t. -k3 |tail&nbsp…

    Linux干貨 2016-08-07
  • grep與正則表達式

    grep與正則表達式 grep與正則表達式 grep 正則表達式 grep 使用語法:grep [-abcEFGhHilLnqrsvVwxy][-C<顯示列數>][-e<范本樣式>][-f<范本文件>][范本樣式][文件或目錄…]  功能說明:查找指定文件或標準輸入里符合條件的字符串 常用選項 -a…

    Linux干貨 2016-11-05
  • N_28 正則表達式的一些基本用法

    1、復制/etc/skel目錄為/home/tuser1,要求/home/tuser1及其內部文件的屬組和其它用戶均沒有任何訪問權限。 ~]# cp -r /etc/skel /home/tuser1 ~]# chmod -R -g— -o— /home/tuser1 2、編輯/etc/group文件,添加組hadoop。 ~]#vi…

    Linux干貨 2017-12-23
  • 我的linux學習方法

    目前記住Linux單詞命令過于困難,現在主要記住linux的命令的作用及用處,不記得單詞可以百度查找做多了自然就記住了。

    Linux干貨 2018-03-17
  • 內核參數修改 內核編譯 第14天

    Linux內核:單內核,模塊化 內核的某些模塊 編譯進內核本體 [*] 編譯成內核模塊 [M] 不選擇使用     [ ] 內核的組成部分 /boot/vmlinuz-VERSION /lib/modules/VERSION/ *.ko 模塊間有可能有依賴關系 內核模塊管理 lsmod:顯…

    Linux干貨 2016-01-18
  • 26期全程班-第二周博客作業

    1、Linux上的文件管理類命令都有哪些,其常用的使用方法及其相關示例演示。     cp  復制命         ex:cp -a /etc/initab /var/log/message /tmp/   …

    Linux干貨 2017-01-10
欧美性久久久久