How can write infinite shell that will run forever? May be I need a 5 second sleep between steps. Recently I have a issue with my routing table and I need to remove some route automatically every 5 minutes. I came up with a solution that runs every 4 minutes and clears unwanted routes.
如何编写将永远运行的无限shell? 在步骤之间可能需要5秒钟的睡眠时间。 最近,我的路由表出现问题,我需要每5分钟自动删除一些路由。 我想出了一种解决方案,该解决方案每4分钟运行一次,清除不需要的路由。
而无限循环 (While Infinite Loop)
There are a lot of different ways do express our solution one of them is when loop of bash. We will provide true
to the while
.
有很多不同的方式向你表达我们对他们的解决方案之一是庆典的时候循环。 我们将提供true
的while
。
$ while true; do echo "test"; sleep 5; done

Here while true runs loop forever between do and done is run regularly but sleep 5 makes the script or each step to wait 5 seconds. We can cancel script with CTRL+C.
在这里,虽然true运行永远在do和done之间循环,但定期运行,但是sleep 5使脚本或每个步骤等待5秒。 我们可以使用CTRL + C取消脚本。
对于无限循环 (For Infinite Loop)
We can also implement infinite loops with the for
. We will provide ( ; ; )
to the for loop which means infinite loop. We will print test
to the console forever with 5 seconds intervals.
我们还可以使用for
实现无限循环。 我们将为for循环提供( ; ; )
,这意味着无限循环。 我们将每隔5秒将test
永久打印到控制台上。
$ for (( ; ; )) do echo "test"; sleep 5; done

Bash无限循环Shell Linux Infografic(Bash Infinite Loop Shell Linux Infografic)
