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)。
以上內(nèi)容到此介紹,下面介紹下Linux shell循環(huán)命令。
Linux shell循環(huán)命令 while死循環(huán)的用法
作為硬件工程師,偶爾會用到Linux shell編程,這里只將while死循環(huán),有相關(guān)需求的工程師可以參考。
死循環(huán)也就是無限循環(huán),它由 while true (表示條件始終為真)或 while : (表示空表達式)組成,其中冒號(:)等效于無操作,冒號和while之間有空格。實例代碼如下:
#!/bin/bash while true #same to while : do echo "drink more water??!" done
運行結(jié)果如下:
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!!
...
到此這篇關(guān)于Linux命令行,循環(huán)執(zhí)行shell命令的文章就介紹到這了,更多相關(guān)linux shell循環(huán)執(zhí)行命令內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
shell腳本快速刪除服務(wù)器指定目錄下文件(最新方法)
這篇文章主要介紹了shell腳本快速刪除服務(wù)器指定目錄下文件(最新方法),包括刪除4天前的文件及只刪除指定文件,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-07-07shell監(jiān)控腳本實例—監(jiān)控mysql主從復(fù)制
分享一例shell腳本,用于監(jiān)測mysql數(shù)據(jù)庫的主從復(fù)制,有需要的朋友不妨參考學(xué)習(xí)下2013-11-11Shell腳本統(tǒng)計當(dāng)前目錄下目錄和文件的數(shù)量
這篇文章主要介紹了Shell腳本統(tǒng)計當(dāng)前目錄下目錄和文件的數(shù)量,Linux下如何統(tǒng)計當(dāng)前目錄下文件有多少個,目錄又有多少個呢,使用本文腳本即可實現(xiàn),需要的朋友可以參考下2014-12-12