while循环使用的很少,通常守护进程程序或始终循环执行会用到,其余循环运算都用for循环代替。shell
(1)while条件语句 apache
while <条件表达式> do 指令…. done
while循环执行流程对应的逻辑图如图1-1所示:编程
(2)until条件语句 bash
until <条件表达式> do 指令… Done
提示:只循环一次,应用场景很少,了解就好。ide
执行挂起一段时间:sleep1挂起秒,usleep1000000挂起1秒。达到一分钟一次执行脚本就就用定时任务。 函数
方法一:每隔2秒屏幕输出负载值学习
[root@shellbiancheng jiaobenlianxi]# cat while1.sh #!/bin/sh while true do uptime sleep 2 done
提示:while true 表示条件永远为真,所以会一直执行,像死循环同样,咱们称之为守护进程。测试
执行结果以下:日志
[root@shellbiancheng jiaobenlianxi]# sh while1.sh 02:50:13 up 1 day, 11:47, 3 users, load average: 0.00, 0.00, 0.00 02:50:15 up 1 day, 11:47, 3 users, load average: 0.00, 0.00, 0.00
方法二:追加到log日志文件里code
[root@shellbiancheng jiaobenlianxi]# cat while2.sh #!/bin/sh while [ 1 ] 注意[]中括号两端有空格,true和1都表示条件永久成立 do uptime >>./uptime.log sleep 2 done [root@shellbiancheng jiaobenlianxi]# tail -f uptime.log 03:00:24 up 1 day, 11:57, 3 users, load average: 0.01, 0.01, 0.00 03:00:26 up 1 day, 11:57, 3 users, load average: 0.01, 0.01, 0.00
经过在脚本的结尾使用&符号来在后台运行脚本:
[root@shellbiancheng jiaobenlianxi]# sh while2.sh & [1] 3991
提示:在后台永久执行,咱们称之为守护进程模式。
防止客户端执行脚本中断,在后台永久执行的方法:
(1)sh while2.sh & 加&符号,让脚本在后台执行
(2)nohup while2.sh & 用户退出以后继续执行脚本
(3)screen 保持会话
扩展:
bg: 后台运行
fg: 挂起程序
jobs: 显示后台程序
kill(kill %1),killall,pkill: 杀掉进程
crontab:设置定时
ps: 查看进程
pstree: 显示进程
nice: 改变优先级
nohup:用户退出系统以后继续工做
pgrep:查找匹配条件的进程
strace:跟踪一个进程的系统调用状况,若是在工做中某个进程使用率太高能够用strace查看进程系统调用状况,
ltrace:跟踪进程调用库函数的状况。
vmstat:报告虚拟内存统计信息。
[root@shellbiancheng jiaobenlianxi]# cat 1-100.sh #!/bin/sh sum=0 i=1 while [ $i -le 100 ] do ((sum=sum+i)) ((i++)) done echo $sum
[root@shellbiancheng jiaobenlianxi]# cat sum1-100.sh #!/bin/sh i=100 ((sum=i*(i+1)/2)) echo $sum
手机充值10元,每发一次短信(输出当前余额)花费1角5分钱,当余额低于1角5分钱不能发短信,提示余额不足,请充值(能够容许用户继续充值继续发短信),请用while语句实现。
提示:单位换算,统一单位,统一成整数。10元=1000分,1角5分=15分
[root@shellbiancheng jiaobenlianxi]# cat huafei.sh #!/bin/bash HUAFEI=100 YUE=25 if [ -f /etc/init.d/functions ];then . /etc/init.d/functions fi OPTION() { case "$option" in [yY]|[yY][eE][sS]) echo "Send a success" echo $txt >>/var/log/consum.log ((HUAFEI=HUAFEI-YUE)) echo "You're still saving money $HUAFEI" ;; [nN]|[nN][oO]) echo "Abort send, succeed." ;; *) echo "Input error" ;; esac return 0 } CHANGE1() { expr $change + 1 &>/dev/null if [ "$?" -ne "0" -a "$change" != "-1" ];then echo "There are illegal characters, please reenter the amount of recharge." else break fi return 0 } CHANGE() { while true do read -p "Please input the amount you want to recharge:" change CHANGE1 done return 0 } CHANGE2() { ((HUAFEI+=change)) echo "You're still saving money $HUAFEI" } OPTION2() { case "$option2" in [yY]|[yY][eE][sS]) CHANGE CHANGE2 ;; [nN]|[nN][oO]) exit 1 ;; *) echo "Input error, please enter the correct amount." CHANGE CHANGE2 ;; esac return 0 } linzhongniao() { if [ "$HUAFEI" -lt "$YUE" ];then read -p "The balance is insufficient, please recharge[y|n]" option2 OPTION2 fi return 0 } main() { while [ "$HUAFEI" -ge "$YUE" ] do read -p "Please enter the content of the text message:" txt read -p "Confirm send [y|n]" option OPTION linzhongniao done return 0 } main
while按行读文件的方式
[root@shellbiancheng jiaobenlianxi]# cat while_duwenjian1.sh #!/bin/bash i=/home/linzhongniao/tools/access.log exec<$i while read line do echo $line done
[root@shellbiancheng jiaobenlianxi]# cat while_duwenjian2.sh #!/bin/bash i=/home/linzhongniao/tools/access.log cat $i|while read line do echo $line done
[root@shellbiancheng jiaobenlianxi]# cat while_duwenjian3.sh #!/bin/bash i=/home/linzhongniao/tools/access.log while read line do echo $line done<$i
分析apache访问日志,把日志中每行的访问字节数对应的字段数相加,计算出访问量。用while循环实现。朋友们作测试的时候若是没有访问日志能够去网上下载一个访问日志。
[root@shellbiancheng tools]# wc -l access_2013_05_30.log 548160 access_2013_05_30.log [root@shellbiancheng jiaobenlianxi]# cat apachefangwen.sh #!/bin/bash sum=0 RETVAL=0 byte="1024" b="/home/linzhongniao/tools/access_2013_05_30.log" exec <$b while read line do size=`echo $line|awk '{print $10}'|grep -v "-"|tr '\r' ' '` expr $size + 1 &>/dev/null if [ $? -ne 0 ];then ((sum+=size)) fi done echo "${b}:total:${sum}bytes = `expr ${sum} / $byte`KB"
在作计算的地方出现了点问题,修改以前作计算的地方是这样写的((sum+=$size)),处理小文件还行处理大文件的时候就会报错(个人测试文本有54万行),在网上查询了缘由是在$取值的时候括号里面只须要跟变量便可(变量可自行进行计算),不须要在对括号内进行运算的变量在进行取值操做即把括号中的$符号去掉,哎这个小问题折腾了我半天时间啊。
一、while循环的特长是执行守护进程以及咱们但愿循环不退出持续执行的状况,用于频率小于一分钟循环处理,其余的while循环几乎均可以被for循环替代。
二、case语句能够替换if语句,通常在系统启动脚本传入少许固定规则字符串,用case语句。其余普通判断多用if语句。
三、if和for语句最经常使用,其次是while(守护进程),case(服务启动脚本)。
各个语句的应用场景:
条件表达式,简单的判断(文件是否存在,字符串是否为空等)。
if取值判断,不一样值数量较少的状况。
for循环正常的循环处理,最经常使用!
while循环守护进程、无限循环(sleep)。
case服务启动脚本,菜单。
函数逻辑清晰,减小重复语句。