循环执行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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM