在每次循环中,我们检查n的值是否大于5,如果大于5,则跳出循环。这样,当n的值达到5时,循环结束。 除了上面的示例,`while true`还可以与其他条件结合使用,以实现更复杂的逻辑。例如,我们可以结合使用`while true`和`sleep`命令,定时执行某个操作。下面是一个简单的示例: ```bash #!/bin/bash while true do ec...
#!/bin/bash while true do if [ `date +%H` -ge 17 ]; then break # exit loop fi echo keep running ~/bin/process_data done … run other commands here … 总结 永远循环很容易。指定要停止循环的条件却需要花费一些额外的精力。via: networkworld.com/articl ...
while [ 1 ] do 语句 done 格式五 死循环 while [ 0 ] do 语句 done 使用示例 示例一 COUNTER=0 while [ $COUNTER -lt10]; do echo The counter is $COUNTER let COUNTER=COUNTER+1 done [root@jfht ~]#COUNTER=0 [root@jfht ~]#while [ $COUNTER -lt 10 ]; do >echo The counter is $CO...
/bin/bashwhile IFS= read -r linedo echo "$line"done < source.txt > target.txt “` 该脚本会逐行读取source.txt的内容,并将每行内容写入target.txt。 4. 无限循环 有时候需要在脚本中创建一个无限循环,可以使用while true或者while :。例如,下面的脚本会持续执行某个命令,直到用户手动中断: “`bash #...
编写while循环,输入q退出(不输入q,不退出) #/bin/bash#whiletrue;doread-p"请输入你的选择"strecho"输入错误"if[$str== q ];thenbreakfidone 编写脚本,每4秒查看系统的内存 #!/bin/bash#whiletrue;douptimesleep3done (3) while read line;do ...
#!/bin/bash # 持续监控应用状态,直到应用运行正常 while true do if systemctl status myapp | grep -q "active (running)"; then break fi sleep 10 done 持续检查服务器负载 #!/bin/bash # 持续检查服务器负载,如果负载过高则发送警报邮件 while true do load=$(uptime | awk '{print $10}'...
while true; do echo $i i=$(($i+1)) if [ $i -gt 10 ]; then break fidone“` 5. foreach循环:foreach循环可用于遍历一个列表或数组,并对每个元素执行一段代码。但需要注意的是,foreach循环在Linux中并不是默认内置的循环结构,而是依赖于具体的命令或脚本语言。例如,在Bash脚本中可以使用for循环来...
最简单的永远循环之一是使用while命令,后面跟上条件true。 你不必使用诸如while [ 1 -eq 1 ]之类的逻辑或类似的测试。while true测试表示循环将一直运行,直到你使用CTRL-C停止循环、关闭终端窗口或注销为止。这是一个例子: 复制 $whiletrue >do >echoKeeprunning ...
while true do 语句 done 格式三 死循环 while : do 语句 done 格式四 死循环 while [ 1 ] do 语句 done 格式五 死循环 while [ 0 ] do 语句 done 使用示例 示例一 Bash代码 COUNTER=0 while [ $COUNTER -lt10]; do echo The counter is $COUNTER ...
/bin/bash#每隔3秒钟到系统上获取已经登录的用户的信息;如果docker用户登录,则记录于日志中,并退出脚本#author chawan#date:20160906whiletrue;doifwho|grep"^docker\>"$>/dev/null;thenbreakfisleep3echo"docker is not login"doneecho"docker logged on.">>/tmp/user.log...