Linux命令行循环执行shell命令(linux循环语句)这都可以

随心笔谈12个月前发布 admin
85 0



目录Linux命令行,循环执行shell命令死循环命令格式效果普通计数循环循环10次效果Linux shell循环命令 while死循环的用法
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:~$

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

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

mycount计数到10后停止循环。

以上内容到此介绍,下面介绍下Linux shell循环命令。

作为硬件工程师,偶尔会用到Linux shell编程,这里只将while死循环,有相关需求的工程师可以参考。

死循环也就是无限循环,它由 while true (表示条件始终为真)或 while : (表示空表达式)组成,其中冒号(:)等效于无操作,冒号和while之间有空格。实例代码如下:

#!/bin/bash
while true #same to while :
do
echo “drink more water!!”
done

运行结果如下:

root@nihao:~# https://www.jb51.net/article/nihaoaaaa.sh

drink more water!!

drink more water!!

drink more water!!

drink more water!!

drink more water!!

drink more water!!

drink more water!!

drink more water!!

到此这篇关于Linux命令行,循环执行shell命令的文章就介绍到这了,更多相关linux shell循环执行命令内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:node.js在Linux下执行shell命令、.sh脚本的问题在docker中执行linux shell命令的操作在linux shell脚本中root切换到普通用户执行脚本或命令的方法linux下执行shell命令方法简介Python2.x利用commands模块执行Linux shell命令Ruby中执行Linux shell命令的六种方法详解linux shell命令执行顺序的控制方法

© 版权声明

相关文章