shell腳本之until循環

一、用until實現下列腳本

1、每隔3秒鐘到系統上獲取已經登錄的用戶的信息;如果發現用戶hacker登錄,則將登錄時間和主機記錄于日志/var/log/login.log,并提示該用戶退出系統。

#!/bin/bash
#author:jackCui
#description:Find out if the system has a hacker user login per 3s
until false ;do
whether=0
  user=`who|grep  "hacker"|cut -f1 -d " "`
  if [ -n "$user" ];then
    until [  $whether -ne 0  ] ;do
    who | grep "hacker">>/var/log/login.log
    echo "hacker logout right now!"| write hacker
     whether=1
     sleep 3
      done
    else
      sleep 3
  fi
done

2、隨機生成10以內的數字,實現猜字游戲,提示比較大或小,相等則退出

[root@centos7 testdir]# cat guess_BorS.sh
#!/bin/bash
#author:jackcui
random=$[$RANDOM%10]
read -p "Input you number: " guess
until [ $random -eq $guess ];do
    if [ $random -gt $guess ];then
      echo "The number you guess is too small"
    else
       echo "The number you guess is too big"
    fi
    read -p "Input you number: " guess
done
  echo "YOU WIN THE GAME!"

3、編寫腳本,求100以內所有正整數之和

[root@centos7 testdir]# cat until100.sh
#!/bin/bash
#author:jackcui
#description:use until solve  sum 1 to N
i=0
alpha=0
sum=0
while [ $alpha -eq 0 ];do      //如果輸入錯誤則循環輸入
   read -p "Input the number:" N
   alpha=`echo $N |grep "\<[[:digit:]]\+\>"|wc -l`
   if [ $alpha -eq 0 ];then
     echo "input error,input must digit!"
   fi
done
 
until [ "$((N+1))" -eq "$i" ];do    //N+1保證能保證最后一個數字也能加上
          ((sum+=i++))
done
echo "The sum 1 to $N is $sum"

4、編寫腳本,通過ping命令探測172.16.250.1-254范圍內的所有主機的在線狀態,統計在線主機和離線主機各多少。

#!/bin/bash
#
i=1
until [ $i -gt 254 ];
do
  if ping -W1 -c1 "172.16.20.$i" &> /dev/null;then
     echo "172.16.20.$i is online!!!!"
     let online++
 else
     echo "172.16.20.$i is not online!!!!"
     let unline++
  fi
    let i++
done
echo "online=$online"
echo "unline=$unline"

5、編寫腳本,打印九九乘法表

#!/bin/bash
i=1;j=1;sum=0
until [ $i -gt 9 ];do
   until [ $j -gt $i ];do
      ((sum=j*i))
      echo -ne "$j*$i=$sum\t"
     ((j++))
   done
    echo ""
 ((j=1))
 ((i++))
done

6、編寫腳本,利用變量RANDOM生成10個隨機數字,輸出這個10數字,并顯示其中的最大者和最小者

[root@centos7 testdir]# cat maxmin.sh
#!/bin/bash
#author:jackcui
temp=$[$RANDOM%20];max=$temp;min=$temp;i=0
until [ $i -eq 10 ];do
   temp=$[$RANDOM%20]
   if [ $temp -ge $max ];then
      max=$temp
  elif [ $temp -lt $min ];then
      min=$temp
   fi
    ((i++))
done
 echo "The maximum value is : $max,minimum value is : $min"

 

7、編寫腳本,實現打印國際象棋棋盤

[root@centos7 testdir]# cat untilchess.sh
#!/bin/bash
#author:jackcui
i=1;j=1
until [ $i -eq 9 ];do
   until [ $j -eq 9 ];do
    if [ $[(i+j)%2] -eq 0  ];then
     echo -en "\e[45m  \e[0m"
       ((j++))
      else
       echo -en "\e[47m  \e[0m"
       ((j++))
     fi
   done
   echo "";((i++));((j=1))
done

8、打印等腰三角形

#!/bin/bash
#author:jackcui
read -p "Input the line you want print: " line
nline=1
i=0;j=0
until [ $nline -eq $[line+1] ];do
  until [ $[line-nline-i ] -eq 0 ];do
    echo -n " "
    ((i++))
  done
    i=0
 until [ $[ 2*nline-1-$j ] -eq 0 ];do
   echo -ne "\e[35;5m*\e[0m"
    ((j++))
    done
     j=0
   ((nline++))
    echo ""
done

 

 

 

 

 

 

 

原創文章,作者:jack_cui,如若轉載,請注明出處:http://www.www58058.com/37198

(0)
jack_cuijack_cui
上一篇 2016-08-18
下一篇 2016-08-18

相關推薦

  • tcp socket文件句柄泄漏

    今天發現有臺redis機器上出現socket個數告警,這是很奇怪的現象。因為一臺redis服務器上就部署了幾個redis實例,打開的端口應該是有限。 1、netstat顯示的tcp連接數正常 netstat -n | awk '/^tcp/ {++state[$NF]} END …

    Linux干貨 2016-04-13
  • 用戶、組、權限和修改

    用戶、組和權限     普通用戶 1–65535     系統用戶  系統已經已經存在專門用來對系統服務或者系統資源進行管理的               1…

    Linux干貨 2017-04-04
  • 搜索引擎-倒排索引基礎知識

    1.單詞——文檔矩陣        單詞-文檔矩陣是表達兩者之間所具有的一種包含關系的概念模型,圖3-1展示了其含義。圖3-1的每列代表一個文檔,每行代表一個單詞,打對勾的位置代表包含關系。          …

    Linux干貨 2015-12-10
  • 一起學習吧:SDCC 2017即將在上海共話架構、數據和運維!

    2017活動家邀你一起學習吧!SDCC 2017在上海共話架構、數據和運維,還有兩天!【召開時間為:3月17至3月19日】 在互聯網大潮下,2017年的就業環境越發惡劣,技術人如何去適應技術變革和學習新技術,以及如何快速增強自身的技術實力成為亟需解決的問題。如果你在看完一本技術圖書后還是疑惑不解,處于目前項目遇到困難時無人請教的尷尬境地,亦不清楚業界同行在使…

    2017-03-15
  • shell腳本編程3

    補充,shift使用方法 root@localhost wang]# cat jiaoben1.sh  #!/bin/bash echo "$1" echo "$*" shift echo "$1" echo &quot…

    Linux干貨 2016-08-24
  • LVM-邏輯盤卷管理

    LVM   一、簡介         LVM是邏輯盤卷管理(Logical Volume Manager)的簡稱,它是Linux環境下對磁盤分區進行管理的一種機制,LVM是建立在硬盤和分區之上的一個邏輯層,來提高磁盤分區管理的靈活性。   二、原理   &nb…

    Linux干貨 2016-04-12
欧美性久久久久