我是条汉子 发表于 2018-8-27 11:09:46

Shell脚本循环语句——until循环

  until循环
  使用于命题为假时执行循环的环境
  命题为假时进入循环;命题为真时退出循环
  或者说是条件不满足时就进入循环的的场景
  从逻辑判断上讲,until和while刚好相反
  语法格式:
  until CONDITION; do
  循环体
  done
  进入条件: CONDITION 为false
  退出条件: CONDITION 为true
  until无限循环写法
  until false; do
  循环体
  done
  until循环应用实例:
  1,计算1-100之间所有正整数的和
  #!/bin/bash
  #Author:wangjun
  #Contact QQ:183530300
  #Version:1.0
  #Create time:2016-08-17 17:08:54
  #Description:1-100 all positive integer's sum
  declare -i I=1
  declare -i sum=0
  until [[ $I -gt 100 ]];do
  let sum+=$I
  let I++
  done
  echo "1-100 all positive integer's sum : $sum"
  2,自动扫描1-254网段的所有主机,统计在线主机数与不在线主机数
  #!/bin/bash
  #Author:wangjun
  #Contact QQ:183530300
  #Version:1.0
  #Create time:2016-08-17 16:30:28
  #Description:ping all host and count the number
  declare -i online=0
  declare -i offline=0
  declare -i host=1
  until [[ $host -ge 255 ]];do
  ping -c 1 -W 1 10.1.250.$host &> /dev/null && echo "10.1.250.$host is online" && let online=$online+1 || let offline=$offline+1
  let host=$host+1
  done
  echo "Online hosts number : $online"
  echo "Offline hosts number : $offline"
  3,生成10个随机数,并找出最大随机数和最小随机数
  #!/bin/bash
  #Author:wangjun
  #Contact QQ:183530300
  #Version:1.0
  #Create time:2016-08-17 20:44:27
  #Description:Random number comparative
  minrandom=$RANDOM
  maxrandom=$minrandom
  declare -i R=1
  echo $minrandom
  until [[ $R -gt 9 ]];do
  RD=$RANDOM
  echo $RD
  if [[ $RD -gt $maxrandom ]];then
  maxrandom=$RD
  elif [[ $RD -lt $minrandom ]];then
  minrandom=$RD
  fi
  let R++
  done
  echo "Minrandom is : $minrandom"
  echo "Maxrandom is : $maxrandom"

  4,打印九九乘法表
  #!/bin/bash
  #Author:wangjun
  #Contact QQ:183530300
  #Version:1.0
  #Create time:2016-08-16 21:57:22
  #Description:99 multiplication tab
  declare -i Row=1
  declare -i Column=1
  until [[ $Row -gt 9 ]];do
  until [[ $Column -gt $Row ]];do
  echo -ne "${Column}x${Row}=$[$Column*${Row}]\t"
  let Column++
  done
  Column=1
  let Row++
  echo
  done

  5,每隔3秒钟查询系统上已经登录的用户信息,如果发现hacker用户登录,则将登录时间和主机记录于日志/var/log/login.log中,并提示hacker用户退出系统
  #!/bin/bash
  #Author:wangjun
  #Contact QQ:183530300
  #Version:1.0
  #Create time:2016-08-17 22:36:44
  #Description:Monitor whether the hacker user has logged on current system
  until who | grep "^hacker\>";do
  echo -e "`date +%T`\thacker user doesn't logged on"
  sleep 3
  done
  echo -e "`date +%T`\thacker user logged on" | tee /var/log/login.log
  echo "Please exit current system" | write hacker
  6,until无限循环用法实例——每隔3秒钟查询系统上已经登录的用户信息,如果发现hacker用户登录,则将登录时间和主机记录于日志/var/log/login.log中,并提示hacker用户退出系统
  #!/bin/bash
  #Author:wangjun
  #Contact QQ:183530300
  #Version:1.0
  #Create time:2016-08-18 08:14:07
  #Description:Monitor whether the hacker user has loggedon
  until false;do
  who | grep "^hacker\>" && echo "Please exit current system" | write hacker && who | grep "^hacker\>" && who | grep "^hacker\>" >> /var/log/login.log || echo -e "`date +%T`\thacker user doesn't logged on"
  sleep 3
  done

页: [1]
查看完整版本: Shell脚本循环语句——until循环