格式
case $re in
y|Y|yes)
echo “xxxx”;;
n|N|no)
echo “xxxx”
;;
*)
echo ‘任意的’
esac
案例1:批量删除用户
#!/bin/bash#案例:批量删除用户(if判断也可)#1.先获取删除用户的前缀#2.再获取用户的数量read -p "请输入用户名前缀" preread -p "请输入数量" numfor i in `seq ${num}`doecho ${pre}${i}doneread -p "以上确认删除吗?y/n" resultfor n in `seq $num`douser=${pre}${n}case $result iny)id ${user} >/dev/null 2>&1if [ $? -eq 0 ];thenuserdel -r ${user} >/dev/null 2>&1elseecho "${user}不存在"fi;;n)echo "退出"exitesacdone
案例2:要求使用菜单显示
#!/bin/bashcat <<-EOF#1. help帮助 打印菜单#2,显示内存使用#3,显示磁盘使用#4,显示系统负载#5,显示登陆用户#6,查看IP地址#7, 查看Linux-version#8, 退出EOFwhile truedoread -p "请输入要查询编号" numcase ${num} in1) ;;2)useFree=`free -h|awk 'NR==2{print $3}'`totalFree=`free -h|awk 'NR==2{print $2}'`echo "内存总大小:${totalFree},已使用:${useFree}";;3)useFdisk=`df -h|awk 'NR==6{print $3}'`totalFdisk=`df -h|awk 'NR==6{print $2}'`echo "磁盘总大小:${totalFdisk},已使用:${useFdisk}";;4) load=`uptime|awk -F '[ ,]' '{print $16}'`echo "当前一分钟负载为:${load}%";;5) user=`whoami`echo "当前用户是:${user}";;6) ip=`curl cip.cc|awk 'NR==1{print $3}'`echo "当前ip地址:${ip}";;7) version=awk '{print $4}' /etc/redhat-releaseecho "当前版本:${version}";;8)echo "退出"exitesacdone
