linux中流程控制语句if if else case

作者:简简单单 2011-12-19

流程控制语句:
    if语句格式如下:
    #if语句的后面是Shell命令,如果该命令执行成功返回0,则执行then后面的命令。
  

 代码如下 复制代码
  if command       
    then
        command
        command
    fi

    #用test命令测试其后面expression的结果,如果为真,则执行then后面的命令。
  

 代码如下 复制代码
  if test expression
    then
        command
    fi

    #下面的格式和test expression等同
  

 代码如下 复制代码
  if [ string/numeric expression ]
    then
        command
    fi

    #下面的两种格式也可以用于判断语句的条件表达式,而且它们也是目前比较常用的两种。
   

 代码如下 复制代码
if [[ string expression ]]
    then
        command
    fi
   
    if (( numeric expression ))           #let表达式
    then
        command
    fi

    见如下示例:
 

 代码如下 复制代码
   /> cat > test1.sh                       #从命令行直接编辑test1.sh文件。
    echo -e "Are you OK(y/n)? c"
    read answer

    #这里的$answer变量必须要用双引号扩住,否则判断将失败。当变量$answer等于y或Y时,支持下面的echo命令。

 代码如下 复制代码
    if [ "$answer" = y -o "$answer" = Y ]  
    then
            echo "Glad to see it."
    fi
    CTRL+D 
    /> . ./test1.sh
    Are you OK(y/n)? y
    Glad to see it.

    上面的判断还可以替换为:

 代码如下 复制代码
    /> cat > test2.sh
    echo -e "Are you OK(y/n or Maybe)? c"
    read answer

    # [[ ]]复合命令操作符允许其中的表达式包含元字符,这里输入以y或Y开头的任意单词,或Maybe都执行then后面的

 代码如下 复制代码
echo。
    if [[ $answer == [yY]* || $answer = Maybe ]] 
    then
        echo "Glad to hear it.
    fi
    CTRL+D
    /> . ./test2.sh
    Are you OK(y/n or Maybe)? yes
    Glad to hear it.


    下面的例子将使用Shell中的扩展通配模式。

 代码如下 复制代码
    /> shopt -s extglob        #打开该扩展模式
    /> answer="not really"
    /> if [[ $answer = [Nn]o?( way |t really) ]]
    > then
    >    echo "I am sorry."
    > fi
    I am sorry.

    对于本示例中的扩展通配符,这里需要给出一个具体的解释。[Nn]o匹配No或no,?( way|t really)则表示0个或1个( way或t really),因此answer变量匹配的字符串为No、no、Not really、not really、No way、no way。
    下面的示例使用了let命令操作符,如:

 代码如下 复制代码

    /> cat > test3.sh
    if (( $# != 2 ))                    #等同于 [ $# -ne 2 ]
    then
            echo "Usage: $0 arg1 arg2" 1>&2
            exit 1                         #exit退出值为0-255之间,只有0表示成功。
    fi
    if (( $1 < 0 || $1 > 30 ))      #等同于 [ $1 -lt 0 -o $1 -gt 30 ]
    then
            echo "arg1 is out of range."
            exit 2
    fi
    if (( $2 <= 20 ))                  #等同于 [ $2 -le 20 ]
    then
            echo "arg2 is out of range."
    fi
    CTRL+D
    /> sh ./test3.sh
    Usage: ./test3.sh arg1 arg2
    /> echo $?                          #Shell脚本的退出值为exit的参数值。
    1
    /> sh ./test3.sh 40 30
    arg1 is out of range.
    /> echo $?


    2
    下面的示例为如何在if的条件表达式中检验空变量:

 代码如下 复制代码

    /> cat > test4.sh
    if [ "$name" = "" ]                #双引号就表示空字符串。
    then
        echo "name is null."
    fi
    CTRL+D
    /> . ./test4.sh
    name is null.


    
    if/elif/else语句的使用方式和if语句极为相似,相信有编程经验的人都不会陌生,这里就不在赘述了,其格式如下:
   

 代码如下 复制代码
if command
    then
        command
    elif command
    then
        command
    else
        command
    fi

    见如下示例脚本:

 代码如下 复制代码

    /> cat > test5.sh
    echo -e "How old are you? c"
    read age
    if [ $age -lt 0 -o $age -gt 120 ]                #等同于 (( age < 0 || age > 120 ))
    then
            echo "You are so old."
    elif [ $age -ge 0 -a $age -le 12 ]               #等同于 (( age >= 0 && age <= 12 ))
    then
            echo "You are child."
    elif [ $age -ge 13 -a $age -le 19 ]             #等同于 (( age >= 13 && age <= 19 ))
    then
            echo "You are 13--19 years old."
    elif [ $age -ge 20 -a $age -le 29 ]             #等同于 (( age >= 20 && age <= 29 ))
    then
            echo "You are 20--29 years old."
    elif [ $age -ge 30 -a $age -le 39 ]             #等同于 (( age >= 30 && age <= 39 ))
    then
            echo "You are 30--39 years old."
    else
            echo "You are above 40."
    fi
    CTRL+D
    /> . ./test5.sh
    How old are you? 50
    You are above 40.


    
    case语句格式如下:
    case variable in
    value1)
        command
        ;;            #相同于C语言中case语句内的break。
    value2)
        command
        ;;
    *)                #相同于C语言中switch语句内的default
        command
        ;;
    esac
    见如下示例脚本:

 代码如下 复制代码
    /> cat > test6.sh
    #!/bin/sh
    echo -n "Choose a color: "
    read color
    case "$color" in
    [Bb]l??)
            echo "you select blue color."
            ;;
    [Gg]ree*)
            echo "you select green color."
            ;;
    red|orange)
            echo "you select red or orange."
            ;;
    *)
            echo "you select other color."
            ;;
    esac
    echo "Out of case command."
    /> . ./test6.sh
    Choose a color: green
    you select green color.
    Out of case command.

相关文章

精彩推荐