1.循环 for/do/done
shell脚本的for循环结构和C语言不一样,它类似于某些编程语的foreach循环。
- #!/bin/bash
- for FRUIT in apple banana pear; do
- echo "I like $FRUIT"
- done
FRUIT(可自定义变量)是一个循环变量,第一次循环$FRUIT的取值是apple,第二次取值是banana,第三次取值是pear
while/do/done
while的用法和C语言类似。比如一个验证密码的脚本:
- #!/bin/bash
-
- echo "请输入密码:"
- read PASSWORD
- while [ "$PASSWORD" != "123456" ];do
- echo "sorry,try again