for…in…do…done

格式

  1. for var in val1 val2 val3
  2. do
  3. 语句1
  4. 语句2
  5. ...
  6. done

示例:

  1. for i in a 2 cc
  2. do
  3. echo $i
  4. done
  5. for file in `ls`
  6. do
  7. echo $file
  8. done
  9. for i in $(seq 1 10)
  10. do
  11. echo $i
  12. done
  13. for i in {a..z}
  14. do
  15. echo $i
  16. done

for ((…;…;…)) do…done

格式:

  1. for ((expression; condition; expression))
  2. do
  3. 语句1
  4. 语句2
  5. done

示例:

  1. for ((i=1; i<=10; i++))
  2. do
  3. echo $i
  4. done

while…do…done

格式:

  1. while condition
  2. do
  3. 语句1
  4. 语句2
  5. ...
  6. done

示例:

  1. while read name
  2. do
  3. echo $name
  4. done

until…do…done

格式:

  1. until condition
  2. do
  3. 语句1
  4. 语句2
  5. ...
  6. done

示例:

  1. until [ "${word}" == "yes" ] || [ "${word}" == "YES" ]
  2. do
  3. read -p "Please input yes/YES to stop this program: " word
  4. done

死循环的处理方式

如果AC Terminal可以打开该程序,则输入Ctrl+c即可。

否则可以直接关闭进程:
使用top命令找到进程的PID
输入kill -9 PID即可关掉此进程