我们经常需要在shell里面做循环操作,比如每隔几秒检查状态, 或者做什么事; 下面的代码例子,每隔5秒做一件事,每隔10秒又做一件事情.
$ cat loop.sh
#!/bin/bash
function fun5() {
echo "in fun5($1)"
}
function fun10() {
echo "in fun10($1)"
}
typeset count=0
while true; do
sleep 1
echo "count=${count}"
if [ `expr ${count} % 5` -eq 0 ]; then
fun5 ${count}
fi
if [ `expr ${count} % 10` -eq 0 ]; then
fun10 ${count}
fi
((count=count + 1))
done
运行结果:
$ ./loop.sh
count=0
in fun5(0)
in fun10(0)
count=1
count=2
count=3
count=4
count=5
in fun5(5)
count=6
count=7
count=8
count=9
count=10
in fun5(10)
in fun10(10)
count=11
count=12
count=13
count=14
count=15
in fun5(15)
count=16
count=17
...