循环执行shell命令

Linux命令行,循环执行shell命令

死循环

命令格式

while true ;do <command>; done;

可以将 command 替换为任意命令。
下面以echo “hello”; sleep 1;为 command 展示最终效果

wanghan@ubuntu:~$ while true ;do echo "hello"; sleep 1; done;
hello
hello
hello
hello
hello
^C
wanghan@ubuntu:~$
1
2
3
4
5
6
7
8

每隔一秒,打印一次hello,直到按下Ctrl+C才停止。

普通计数循环

循环10次

mycount=0; while (( $mycount < 10 )); do <command>;((mycount=$mycount+1)); done;

可以将 command 替换为任意命令。
下面以 echo “mycount=$mycount”;为 command 展示最终效果

wanghan@ubuntu:~$ mycount=0; while (( $mycount < 10 )); do echo "mycount=$mycount"; ((mycount=$mycount+1)); done;
mycount=0
mycount=1
mycount=2
mycount=3
mycount=4
mycount=5
mycount=6
mycount=7
mycount=8
mycount=9
1
2
3
4
5
6
7
8
9
10
11

mycount计数到10后停止循环。


原文链接:https://blog.csdn.net/daoshuti/article/details/72831256

原文地址:https://www.cnblogs.com/soymilk2019/p/14607652.html