1. 程式人生 > >while循環語句

while循環語句

cat 語句 ptime 滿足 until lee roo 死循環 退出

while條件句:條件滿足一直執行。

語法:

while 條件

  do

  指令

done

untile條件句:條件滿足就退出(不常用了解)

until 條件

  do

  指令

done

案例一:每隔2秒查看下服務器的負載相當於top命令

[[email protected] while]# cat while1.sh
#!/bin/bash
while true
do
uptime
sleep 2
done

#while true 表示永久為真,一直執行,死循環(守護進程)。

執行結果:

10:53:06 up 18:37, 2 users, load average: 0.08, 0.23, 0.14

案例二:

#!/bin/bash
while true
do
uptime >> /root/while/test.log
# usleep 500 #微秒
usleep 10000000
done

擴展:

後臺執行的方法:

[[email protected] while]# sh while2.sh &
[1] 54786

查看後臺正在運行的進程

[[email protected] while]# jobs
[1]+ Running sh while2.sh &

將後臺運行的放到前臺執行

[[email protected]

/* */ while]# fg 1
sh while2.sh

  

while循環語句