[md]在Linux shell脚本中,除了使用 `do` 关键字的 `while` 和 `until` 循环外,还有以下几种常用的循环命令: 1. **for 循环**: ```bash for variable in list do # Commands to execute for each item in list done ``` 2. **C-style for 循环**(在某些shell如bash中): ```bash for (( initialization; condition; increment )) do # Commands to execute done ``` 3. **select 循环**(类似于C语言的switch-case): ```bash select name [in list] do case $name in pattern1 ) # Commands to execute if name matches pattern1 ;; pattern2 ) # Commands to execute if name matches pattern2 ;; * ) # Default commands to execute ;; esac done ``` 4. **case 语句**(虽然不是循环,但可以用于基于条件的迭代): ```bash case expression in pattern1 ) # Commands to execute if expression matches pattern1 ;; pattern2 ) # Commands to execute if expression matches pattern2 ;; * ) # Default commands to execute ;; esac ``` 5. **if 语句**(虽然不是循环,但常用于条件判断): ```bash if [ condition ] then # Commands to execute if condition is true elif [ another condition ] then # Commands to execute if the first condition is false and another condition is true else # Commands to execute if all conditions are false fi ``` 6. **函数**(虽然不是循环命令,但可以包含循环): ```bash function my_function { # Commands to execute, can include loops } ``` 7. **循环控制语句**(如 `break` 和 `continue`,它们不是循环本身,但用于控制循环的流程): - `break`:退出最近的 `for`、`while`、`until` 或 `select` 循环。 - `continue`:跳过当前循环的剩余部分,并开始下一次迭代。 这些是Linux shell脚本中常用的循环和条件判断结构,它们允许你执行基于条件或列表项的重复任务。 [/md] |