在Linux中,done關鍵字通常用于結束一個循環結構,如for循環、while循環或until循環。在使用這些循環結構時,需要使用do關鍵字來開始循環體,然后使用done關鍵字來結束循環體。示例代碼如下:
#!/bin/bash
# 使用for循環遍歷數組
fruits=("apple" "banana" "cherry")
for fruit in "${fruits[@]}"
do
echo "I like $fruit"
done
# 使用while循環打印數字
num=1
while [ $num -le 5 ]
do
echo $num
num=$((num + 1))
done
# 使用until循環倒計時
countdown=10
until [ $countdown -eq 0 ]
do
echo "$countdown seconds left"
countdown=$((countdown - 1))
done
在上面的示例中,done關鍵字被用于結束for、while和until循環結構,確保循環體內的命令會被正確執行。在編寫shell腳本時,記得在每個循環結構的結束處使用done關鍵字。