while
sh xxxx & ##加上&符号,代表在后台执行
[root@server1 test]# sh fuzai.sh &
[1] 10019 ##在后台执行
[root@server1 test]# jobs
[1]+ Running sh fuzai.sh & ##查看后台运行程序
[root@server1 test]# fg 1 ##fg加编号调在前台执行
sh fuzai.sh
^C ##ctrl+c停止执行并关闭;ctrl+z暂停执行,还在jobs队列之中,bg恢复执行.
实例1:1加到100
#!/bin/bash
i=1
sum=0
while (( i <= 100 ))
do
let sum=$sum+$i
((i++))
done
printf "$sum\n"
[root@server1 test]# seq 10|tac
10
9
8
7
6
5
4
3
2
1
##加tac倒叙
#!/bin/bash
i=10
while (( i >= 1 ))
do
echo $i
((i--))
done
监控web服务案例
#!/bin/bash
while true
do
if [[ ` curl -o /dev/null -s -w "%{http_code}" http://www.baidu.com` -ne 200 ]] ##检查返回值是否是200
then
echo "this is error!"
else
echo "it's right!"
fi
sleep 2
done