echo ---------- while 案例—循环求1-100的总和 ----------
j=0i=1while((i<=100))doj=`expr $i + $j`((i++))doneecho$j
3.3 while 案例——循环每 10 秒判断系统登录
echo ---------- while 案例——循环每`10`秒判断系统登录 ----------
# `root用户`是否登录系统。USERS="qs"whiletruedoecho"The Time is `date +%F-%T`"sleep10NUM=`who|grep"$USERS"|wc -l`if[[$NUM -ge 1]];thenecho"The $USERS is login in system."fidone
# 1、`select语句`是一个类似于`for循环`的语句。# 2、`select语句`一般用于选择,常用于选择菜单的创建。# 3、可以配合`PS3`,来做打印菜单的输出信息。# 4、其语法格式以`select ... in do`开头,`done`结尾。selectiin('表达式')do'语句'done
5.1 select 案例——选择 MySQL 版本
echo ---------- select 案例——选择MySQL版本 ----------
PS3="Select a number: "whiletrue;doselectmysql_versionin5.15.6 quit;docase$mysql_versionin5.1)echo"mysql 5.1"break;;5.6)echo"mysql 5.6"break;;
quit)exit;;
*)echo"Input error, Please enter again!"breakesacdonedone
5.2 select 案例——打印 lnmp 菜单栏
echo ---------- select 案例——打印`lnmp`菜单栏 ----------
PS3="Please enter you select install menu: "selectiin http php mysql quit
docase$iin
http)echo -e "\033[31m Test Httpd \033[0m";;
php)echo -e "\033[32m Test PHP\033[0m";;
mysql)echo -e "\033[33m Test MySQL.\033[0m";;
quit)echo -e "\033[32m The System exit.\033[0m"exitesacdone
echo ---------- case 案例—参数解析 ----------
#!/bin/shdeclare -i win_count=0declare -i lin_count=0functionhelp(){echo -e "\033[36;1mhelp:\033[0m"echo -e "\033[36;1m --win(-W) : Windows Number of Windows VMS(eg: -W 2 or --win 2)\033[0m"echo -e "\033[36;1m --Lin(-L) : Linux Number of Linux VMS(eg: -L 2 or --lin 2)\033[0m"echo -e "\033[36;1m --help(-h): display this help and exit \033[0m"}functionisLegal(){machine_num=$1if[$machine_num -eq 0];thenecho"[-] Parameters of illegal !!"helpexit1fi}if[[$1="--help"]]||[[$1="-h"]];thenhelpexit1fiif[! -n "$1"];thenecho"[-] Parameters of illegal !"helpexit1fiwhile[ -n "$1"]docase"$1"in
--win)win_count=$2shift;;
-W)win_count=$2shift;;
--lin)lin_count=$2shift;;
-L)lin_count=$2shift;;
*)echo"[-] Parameters of illegal !!!"helpexit1;;esacshiftdone
isLegal $win_count
isLegal $lin_countecho$win_count --- $lin_count