和while循环相反,until循环时只要条件判断式不成立则进行循环,一旦循环条件成立,则终止循环 2) until [ 条件判断式 ] do 程序 done 例:计算1到100的和: #!/bin/sh i=1 s=0 until [ $i -gt 100 ] do s=$(( $s + $i )) i=$(( $i + 1 )) done echo "the sum is:$s" 5、case语句 (1)语法: case $变量名 in 值1) 如果变量的值等于值1,则执行程序1 ;; 值2) 如果变量的值等于值2,则执行程序2 ;; …… *) 如果变量的值都不是以上的值,则执行此程序 ;; esac (2)案例: 例1:简单的case判断 #!/bin/sh read -t 30 -p "please choose yes/no:" cho case $cho in "yes") echo "your choose is yes!" ;; "no") echo "you choose is no" ;; *) echo "your choose is error" ;; esac 例2:用case打印水果菜单接受用户选择并输出 #!/bin/sh RED_COLOR='\033[31m' GREER_COLOR='\033[32m' YELLOW_COLOR='\033[33m' RES='\033[0m' while true do menu(){ cat <<END 1.apple 2.pear 3.banana 4.exit END } menu read -p "please input your cheoise:" fruit case "$fruit" in 1) echo -e "$RED_COLOR apple $RES" ;; 2) echo -e "$GREER_COLOR pear $RES" ;; 3) echo -e "$YELLOW_COLOR banana $RES" ;; 4) exit 0 ;; *) echo "no fruit you choose" exit 1 esac done 6、案例 例1、将尝试登陆系统10次失败的用户加入/etc/hosts.deny(脚本完成后使用nohup bah /scripts/ssh_deny.sh &) #!/bin/bash while true do lastb -n 100 | grep -v "btmp" | grep -v "^$" | grep -v "172.16.252.100" | tr -s " " | cut -d" " -f3 | sort | uniq -c | sort -nr > hacker.log while read line;do COUNT=`echo ${line} | awk '{print $1}'` IPADDR=`echo ${line} | awk '{print $2}'` if [[ ${COUNT} -gt 5 ]];then grep -q ${IPADDR} /etc/hosts.deny if [ ! $? -eq 0 ];then echo "sshd:${IPADDR}" >> /etc/hosts.deny fi fi done < hacker.log done 例2:DOS攻击自动防护脚本(执行时需要放入后台如:sh whidos.sh &) #!/bin/sh while true do awk '{print $1}' /var/log/httpd/access_log | grep -v "^$" | sort | uniq -c >/tmp/tmp.log exec </tmp/tmp.log while read line do ip=`echo $line | awk '{print $2}'` count=`echo $line | awk '{print $1}'` if [ $count -gt 20 ] && [ `iptables -L -n | grep "$ip" | wc -l` -lt 1 ] |