04- Shell脚本学习—条件控制

    语法格式:

    注意: 和方括号([ ])之间必须有空格,否则会有语法错误。

    if 语句通过关系运算符判断表达式的真假来决定执行哪个分支。Shell 有三种 if … else 语句:

    1. if ... fi 语句
    2. if ... else ... fi 语句
    3. if ... elif ... else ... fi 语句

    示例:

    1. #!/bin/bash/
    2. a=10
    3. b=20
    4. if [ $a == $b ]
    5. then
    6. echo "a is equal to b"
    7. elif [ $a -gt $b ]
    8. then
    9. echo "a is greater to b"
    10. else
    11. echo "a is less to b"
    12. fi

    if ... else 语句也可以写成一行,以命令的方式来运行:

    1. a=10;b=20;if [ $a == $b ];then echo "a is equal to b";else echo "a is not equal to b";fi;

    if ... else 语句也经常与 test 命令结合使用,作用与上面一样:

    1. #!/bin/bash/
    2. a=10
    3. b=20
    4. if test $a == $b
    5. then
    6. echo "a is equal to b"
    7. else
    8. echo "a is not equal to b"
    9. fi

    分支控制:case语句

    case ... esac 与其他语言中的 switch ... case 语句类似,是一种多分枝选择结构。

    示例:

    1. #!/bin/bash/
    2. grade="B"
    3. case $grade in
    4. "A") echo "Very Good!";;
    5. "B") echo "Good!";;
    6. "C") echo "Come On!";;
    7. *)
    8. echo "You Must Try!"
    9. echo "Sorry!";;
    10. esac

    转换成C语言是:

    1. #include <stdio.h>
    2. int main(){
    3. char grade = 'B';
    4. switch(grade){
    5. case 'A': printf("Very Good!");break;
    6. case 'B': printf("Very Good!");break;
    7. case 'C': printf("Very Good!");break;
    8. printf("You Must Try!");
    9. printf("Sorry!");
    10. break;
    11. return 0;
    12. }

    对比看就很容易理解了。很相似,只是格式不一样。

    需要注意的是:
    取值后面必须为关键字 in,每一模式必须以右括号结束。取值可以为变量或常数。匹配发现取值符合某一模式后,其间所有命令开始执行直至 ;;;; 与其他语言中的 break 类似,意思是跳到整个 case 语句的最后。

    取值将检测匹配的每一个模式。一旦模式匹配,则执行完匹配模式相应命令后不再继续其他模式。如果无一匹配模式,使用星号 * 捕获该值,再执行后面的命令。

    再举一个例子:

    1. #!/bin/bash
    2. option="${1}"
    3. case ${option} in
    4. "-f") FILE="${2}"
    5. echo "File name is $FILE"
    6. ;;
    7. "-d") DIR="${2}"
    8. echo "Dir name is $DIR"
    9. ;;
    10. *)
    11. echo "`basename ${0}`:usage: [-f file] | [-d directory]"
    12. exit 1 # Command to come out of the program with status 1
    13. ;;
    14. esac
    1. $./test.sh
    2. test.sh: usage: [ -f filename ] | [ -d directory ]
    3. ./test.sh -f index.html
    4. File name is index.html

    这里用到了特殊变量${1},指的是获取命令行的第一个参数。

    shell的for循环与c、php等语言不同,同Python很类似。下面是语法格式:

    示例:

    1. #!/bin/bash/
    2. for value in 1 2 3 4 5
    3. do
    4. echo "The value is $value"
    5. done

    输出:

    1. The value is 1
    2. The value is 2
    3. The value is 3
    4. The value is 4
    5. The value is 5

    顺序输出字符串中的字符:

    1. for str in 'This is a string'
    2. do
    3. echo $str
    4. done

    运行结果:

    1. This is a string

    遍历目录下的文件:

    1. #!/bin/bash
    2. for FILE in *
    3. do
    4. echo $FILE
    5. done

    上面的代码将遍历当前目录下所有的文件。在Linux下,可以改为其他目录试试。

    遍历文件内容:
    city.txt

    1. beijing
    2. tianjin
    3. shanghai
    1. #!/bin/bash
    2. citys=`cat city.txt`
    3. for city in $citys

    输出:

    1. beijing
    2. tianjin
    3. shanghai

    while循环

    只要while后面的条件满足,就一直执行do里面的代码块。

    其格式为:

    命令执行完毕,控制返回循环顶部,从头开始直至测试条件为假。

    1. #!/bin/bash
    2. c=0;
    3. while [ $c -lt 3 ]
    4. do
    5. echo "Value c is $c"
    6. c=`expr $c + 1`
    7. done

    输出:

    1. Value c is 0
    2. Value c is 1
    3. Value c is 2

    这里由于shell本身不支持算数运算,所以使用expr命令进行自增。

    until 循环执行一系列命令直至条件为 true 时停止。until 循环与 while 循环在处理方式上刚好相反。一般while循环优于until循环,但在某些时候,也只是极少数情况下,until 循环更加有用。

    将上面while循环的例子改改,就能达到一样的效果:

    1. #!/bin/bash
    2. c=0;
    3. until [ $c -eq 3 ]
    4. do
    5. echo "Value c is $c"
    6. c=`expr $c + 1`
    7. done

    首先do里面的语句块一直在运行,直到满足了until的条件就停止。

    输出:

    1. Value c is 0
    2. Value c is 1
    3. Value c is 2

    跳出循环

    在循环过程中,有时候需要在未达到循环结束条件时强制跳出循环,像大多数编程语言一样,Shell也使用 break 和 continue 来跳出循环。

    break命令允许跳出所有循环(终止执行后面的所有循环)。

    1. #!/bin/bash
    2. i=0
    3. while [ $i -lt 5 ]
    4. do
    5. i=`expr $i + 1`
    6. if [ $i == 3 ]
    7. then
    8. break
    9. fi
    10. echo -e $i
    11. done

    运行结果:

    1. 1
    2. 2

    在嵌套循环中,break 命令后面还可以跟一个整数,表示跳出第几层循环。例如:

    1. break n

    表示跳出第 n 层循环。

    continue命令与break命令类似,只有一点差别,它不会跳出所有循环,仅仅跳出当前循环。

    1. #!/bin/bash
    2. i=0
    3. while [ $i -lt 5 ]
    4. do
    5. i=`expr $i + 1`
    6. if [ $i == 3 ]
    7. then
    8. continue
    9. fi
    10. echo -e $i

    运行结果: