循环:`for`、`while`、`until`
掌握 Shell 脚本中的不同循环类型,自动执行重复性任务。
循环:`for`、`while`、`until` 是 CoddyKit 上的免费 Linux Command Line Mastery 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Linux Command Line Mastery 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Linux Command Line Mastery 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Why Use Loops?
Imagine you need to perform the same action many times, like saying 'Hello' to everyone in a list, or counting from 1 to 100. Doing this manually would be tedious and error-prone.
This is where loops come in! Loops allow your script to repeat a set of commands multiple times, saving you effort and making your scripts more efficient.
Looping Through Items with `for`
The for loop is perfect when you know exactly what items you want to iterate over. It goes through a list, processing each item one by one.
Its basic syntax looks like this:
for item in list; do# commands to run for each itemdone
The item variable will hold each value from the list in turn.
`for` Loop in Action
Let's see the for loop iterate through a list of names. Each name is assigned to the name variable, and then printed.
Try running this example:
#!/bin/bash
echo "--- Greeting Friends ---"
for name in Alice Bob Charlie;
do
echo "Hello, $name!"
done
echo "--- All greeted! ---"`for` Loop for Number Ranges
What if you need to loop a specific number of times, like counting from 1 to 5? You can generate sequences for the for loop:
- Brace Expansion:
{1..5}creates "1 2 3 4 5". seqcommand:$(seq 1 5)also creates "1 2 3 4 5".
Both are handy for numerical iterations.
Counting with `for` Loop
Here's how you can use brace expansion to count up to a specific number. The loop variable i takes on each number in the sequence.
Try running this example:
#!/bin/bash
echo "Counting up!"
for i in {1..3};
do
echo "Count: $i"
done
echo "Done counting."Repeating with `while`
The while loop is used when you want to repeat commands as long as a certain condition remains true. It continuously checks the condition before each iteration.
Its basic structure is:
while condition; do# commands to rundone
The loop stops as soon as the condition becomes false.
`while` Loop Counter
Let's use a while loop to count. We initialize a variable count, and the loop continues as long as count is less than 3.
Remember to update the variable inside the loop, or it might run forever (an 'infinite loop')!
#!/bin/bash
count=0
echo "Starting while loop..."
while [ $count -lt 3 ]; do
echo "Current count: $count"
count=$((count + 1))
done
echo "While loop finished."Repeating `until` True
The until loop is similar to while, but with an inverse condition. It executes commands until a specified condition becomes true.
Its syntax is:
until condition; do# commands to rundone
The loop continues as long as the condition is false.
`until` Loop in Action
Here's an until loop waiting for a variable num to reach 3. It will keep running as long as num is not greater than or equal to 3.
Try running this example:
#!/bin/bash
num=0
echo "Starting until loop..."
until [ $num -ge 3 ]; do
echo "Current number: $num"
num=$((num + 1))
done
echo "Until loop finished."When to Use Which Loop?
Choosing the right loop makes your scripts clearer and more efficient:
forloop: Best for iterating over a known list of items (files, names, numbers in a range).whileloop: Ideal when you need to repeat commands as long as a condition holds true (e.g., reading lines from a file, waiting for user input, counting).untilloop: Useful when you want to repeat commands until a specific condition becomes true (e.g., waiting for a process to finish, retrying an operation).
Loop Challenge
Let's test your understanding of shell loops.
Loops: A Quick Recap
Well done! You've learned the fundamental looping constructs in shell scripting:
- The
forloop iterates over a fixed list of items or a range. - The
whileloop repeats commands as long as a condition is true. - The
untilloop repeats commands until a condition becomes true.
These loops are essential for automating repetitive tasks and making your scripts dynamic and powerful. Keep practicing!
常见问题解答
「循环:`for`、`while`、`until`」课时是免费的吗?
是的 — 「循环:`for`、`while`、`until`」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Linux Command Line Mastery 课程的其余内容,请升级到 CoddyKit PRO。 Linux Command Line Mastery 课程共包含 4 节课。
「循环:`for`、`while`、`until`」这节课中我会学到什么?
掌握 Shell 脚本中的不同循环类型,自动执行重复性任务。 你通过在浏览器中直接运行的动手代码来练习 Linux Command Line Mastery,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Linux Command Line Mastery 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Linux Command Line Mastery 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。
「循环:`for`、`while`、`until`」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Linux Command Line Mastery 课中编写并运行代码吗?
能。每节 Linux Command Line Mastery 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 变量、输入与输出
- 条件语句:`if`、`else`、`case`
- 循环:`for`、`while`、`until`
- Shell 脚本中的函数与参数