
- Unix/Linux初學者教程
- Unix/Linux - 首頁
- Unix/Linux - 什麼是Linux?
- Unix/Linux - 入門指南
- Unix/Linux - 檔案管理
- Unix/Linux - 目錄
- Unix/Linux - 檔案許可權
- Unix/Linux - 環境變數
- Unix/Linux - 基本實用程式
- Unix/Linux - 管道與過濾器
- Unix/Linux - 程序
- Unix/Linux - 通訊
- Unix/Linux - vi編輯器
- Unix/Linux Shell程式設計
- Unix/Linux - Shell指令碼
- Unix/Linux - 什麼是Shell?
- Unix/Linux - 使用變數
- Unix/Linux - 特殊變數
- Unix/Linux - 使用陣列
- Unix/Linux - 基本運算子
- Unix/Linux - 決策語句
- Unix/Linux - Shell迴圈
- Unix/Linux - 迴圈控制
- Unix/Linux - Shell替換
- Unix/Linux - 引號機制
- Unix/Linux - I/O重定向
- Unix/Linux - Shell函式
- Unix/Linux - 手冊頁幫助
- 高階Unix/Linux
- Unix/Linux - 標準I/O流
- Unix/Linux - 檔案連結
- Unix/Linux - 正則表示式
- Unix/Linux - 檔案系統基礎
- Unix/Linux - 使用者管理
- Unix/Linux - 系統性能
- Unix/Linux - 系統日誌
- Unix/Linux - 訊號和陷阱
Unix/Linux Shell - for迴圈
for迴圈用於操作專案列表。它對列表中的每個專案重複執行一組命令。
語法
for var in word1 word2 ... wordN do Statement(s) to be executed for every word. done
這裡,var 是變數名,word1 到 wordN 是以空格分隔的字元序列(單詞)。每次for迴圈執行時,變數var的值都設定為word1到wordN列表中的下一個單詞。
示例
這是一個簡單的例子,它使用for迴圈遍歷給定的數字列表:
#!/bin/sh for var in 0 1 2 3 4 5 6 7 8 9 do echo $var done
執行後,您將收到以下結果:
0 1 2 3 4 5 6 7 8 9
以下示例顯示所有以.bash開頭並位於您主目錄中的檔案。我們將從我的根目錄執行此指令碼:
#!/bin/sh for FILE in $HOME/.bash* do echo $FILE done
上述指令碼將產生以下結果:
/root/.bash_history /root/.bash_logout /root/.bash_profile /root/.bashrc
unix-shell-loops.htm
廣告