Linux命令行循環(huán)執(zhí)行shell命令
Linux命令行,循環(huán)執(zhí)行shell命令
死循環(huán)
命令格式
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才停止。
普通計數(shù)循環(huán)
循環(huán)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
mycount計數(shù)到10后停止循環(huán)。
以上內容到此介紹,下面介紹下Linux shell循環(huán)命令。
Linux shell循環(huán)命令 while死循環(huán)的用法
作為硬件工程師,偶爾會用到Linux shell編程,這里只將while死循環(huán),有相關需求的工程師可以參考。
死循環(huán)也就是無限循環(huán),它由 while true (表示條件始終為真)或 while : (表示空表達式)組成,其中冒號(:)等效于無操作,冒號和while之間有空格。實例代碼如下:
#!/bin/bash while true #same to while : do echo "drink more water!!" done
運行結果如下:
root@nihao:~# ./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命令行,循環(huán)執(zhí)行shell命令的文章就介紹到這了,更多相關linux shell循環(huán)執(zhí)行命令內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Linux命令創(chuàng)建日期文件夾或者文件的實例代碼
本文通過實例代碼給大家介紹了Linux命令創(chuàng)建日期文件夾或者文件的相關知識,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-10-10
Linux設置每晚定時備份Oracle數(shù)據(jù)表的操作命令
這篇文章主要介紹了Linux設置每晚定時備份Oracle數(shù)據(jù)表,本文通過腳本命令給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-12-12
Linux 中shell腳本設置開頭固定格式的實現(xiàn)方法
這篇文章主要介紹了Linux 中shell腳本設置開頭固定格式的實現(xiàn)方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10

