shell程式設計 迴圈語句

2021-10-01 02:55:40 字數 815 閱讀 6639

案例1: 遍歷

#!/bin/bash

for i in `seq 1 15`

do echo $i

done

案例2:for累加

#!/bin/bash

j=0for ((i=0;i<100;i++))

do j=`expr $i + $j`

done

echo $j

案例3:遍歷檔案打包

#!/bin/bash

for i in `find . -name "*.sh"`

do tar czf lcj.tgz $i

done

遍歷當前目錄下以.sh結尾的檔案並打包

案例4:while累加

#!/bin/bash

i=0while [[ $i -lt 100 ]]

do i=`expr $i + 1`

echo $i

done

案例5:讀檔案

#!/bin/bash

while read line

do echo $line

done<./lcj.txt

讀取當前目錄下的lcj.txt檔案並輸出

shell程式設計 迴圈語句

while語句 while語句格式 while 表示式 do command command done while 和 if 的條件表示式完全相同,也是 或commad或test while 表示式 if 表示式 表示式值為0,則迴圈繼續 表示式值為0,then 表示式值為非0,則迴圈停止 表示式值...

shell程式設計 迴圈語句

while語句 while語句格式 while 表示式 do command command done while 和 if 的條件表示式完全相同,也是 或commad或test while 表示式 if 表示式 表示式值為0,則迴圈繼續 表示式值為0,then 表示式值為非0,則迴圈停止 表示式值...

Shell 程式設計迴圈語句

我們可以用 for 結構的迴圈來處理一組值,這組值可以是任意字串的集合。for variable in values do statement donefor foo in aa bb cc do echo foo done exit 0輸出結果是 aabb ccfor 迴圈特別適合對一系列字串進行...