shell 中的 for 語句
語法介紹
for 可以用來遍歷數組,而這個數組很泛用,可以是數字、文件列表或是命令執行的結果
1 |
|
常用範例
遍歷命令執行的結果
1
2
3
4for user in $(ls /home)
do
echo "User: $user"
done遍歷文件列表
1
2
3
4
5# 遍歷當前目錄下 後綴為.txt的文件
for file in *.txt
do
echo "Processing file: $file"
done遍歷數組
1
2
3
4
5
6fruits=("apple" "banana" "cherry")
# [@] 需要這樣寫才能遍歷整個數組
for fruit in "${fruits[@]}"
do
echo "Fruit: $fruit"
done
shell 中的 for 語句
https://austin72905.github.io/2023/11/27/linux-shell-for/