Linux C笔记3——Shell编程之选择语句

来源:互联网 发布:淘宝美工怎么接私活 编辑:程序博客网 时间:2024/05/16 14:06


test命令

    经常用于if语句中测试一种或几种条件
    大多数shell程序会大量使用test命令(另一种写法:[]),即shell的布尔值判断命令
    格式:test 表达式    或       [表达式]
        其中  表达式是要测试的条件,为真则test命令的退出状态为0。为假则test命令的退出状态不为0

    注意事项:
        用test时,test后面要有空格,=前后要有空格。用[]时要注意:[后面要有空格,]前面要有空格,=前后要有空格。用户编写的程序不能与其重名,即使加后缀也不行。

    

            -----------------
               test命令测试
            -----------------
-----------------------------------------------------------------------------------
        [sayer@localhost Shell]$ echo $LOGNAME             #查看用户登录名
        sayer
        [sayer@localhost Shell]$ test "$LOGNAME" = sayer    #用test判断用户登录名是否是sayer
        [sayer@localhost Shell]$ echo $?            #前一条命令退出的状态为0
        0
        [sayer@localhost Shell]$ test "$LOGNAME" = SAYER    #用test判断用户登录名是否是SAYER
        [sayer@localhost Shell]$ echo $?            #前一条命令退出的状态为1
        1
        [sayer@localhost Shell]$ [ "$LOGNAME" = sayer ]        #[]与test用法一致
        [sayer@localhost Shell]$ echo $?
        0

------------------------------------------------------------------------------------


    test命令所判断的表达式通常是由一写=些shell命令支持的操作符组成:字符串操作符、整数操作符、文件操作符、逻辑操作符。
    



 test字符串操作符


    ----------------------------------------------------------------------
      操作符            返回真(退出状态为0)的条件

      string1=string2    字符串1和2的值完全相同

      string1!=string2    字符串string1和2值不同

      string        字符串不为空

      -n string        字符串不为空(并且test能够看到string)

      -z string        字符串string为空(并且test能够看见string)
    ---------------------------------------------------------------------




test整数操作符


    ---------------------------------------------------------------------
        操作符            返回真(退出状态为0)的条件

      int1 -eq int2        int1等于int2

      int1 -ne int2        int1不等于int2

      int1 -gt int2        int1大于int2

      int1 -ge int2     int1大于或等于int2

      int1 -It int2        int1小于int2

      int1 -le int2        int1小于或等于int2

    ---------------------------------------------------------------------

test文件操作符


    ---------------------------------------------------------------------
       操作符            返回真(退出状态为0)的条件

      -b file        file为特殊块文件

      -c file        file为特殊字符文件

      -d file        file为目录名

      -e file        file文件存在

      -f file        file为普通文件名

      -h file        file为链接文件

      -k file        file已经设置了sticky位

      -L file        file为符号链接文件

      -p file        file为命名管道

      -r file        file文件可读

      -S file        file为套接字(Socket)

      -s file        file文件的长度不为0

      -u file        file的set user id(SUID)位已经设置

      -w file        file文件可写

      -x file        file文件可执行
    ----------------------------------------------------------------------




if语句




单选择if语句格式:

    if 条件表达式
    then
        命令序列
    fi

双选择if语句格式:

    if 条件表达式
    then
        命令序列1
    else
        命令序列2
    fi

多选择if语句格式

    if 条件表达式1
    then
        命令序列1
        elif 条件表达式2
        then
            命令序列2
        else
            命令序列3
    fi

    注意事项:
        1.if后要加空格,[后加空格,]前加空格
        2.每次判断条件后的语句都要加then,即“if 条件表达式”和“elif 条件表达式”后面加then
        3.所有字符均为小写,即:if then else elif fi
        4.若then与if或elif一行,主要在then前加分号;
        5.每次选择语句的结束必须加fi

        -----------------------
             if语句例子
        -----------------------

-----------------------------------------------------------------------
  1 #!/bin/bash
  2 #This is sample about fi or Fi
  3
  4 if [ -e /home/sayer/Word/c.cpp ]
  5 then
  6         echo "true"
  7 else
  8         echo "false"
  9 fi      #fi is true
 10         #Fi is false

        -------
          结果
        -------
    ----------------------
    [sayer@fedora shell]$ ./sys_if.sh
    false

-----------------------------------------------------------------------


    -------------------------------------------
       在规定的时间发送问候语程序
    --------------------------------------------


--------------------------------------------------------------------------
  1 #!/bin/sh
  2 #The  program function is say hello in different time
  3 #use "date" command get systemdate's date       用date命令得到系统的日期
  4 #use "cut" command cut out hour or date +%H        用cut 截取时或date +%H
  5 #eg:date|cut -c19-20 or date +%H
  6 hour=$(date +%H)
  7 if [ "$hour" -ge 0 -a "$hour" -le 11 ]
  8 then
  9         echo "Good morning ! "
 10         elif [ "$hour" -ge 12 -a "$hour" -le 18 ]
 11         then
 12                 echo "Good aftermoon ! "
 13         else
 14                 echo "Good evening ! "
 15 fi
        -------
          结果
        -------
    -----------------------
    [sayer@fedora shell]$ ./sys_sayhello.sh
    Good aftermoon !

--------------------------------------------------------------------------


case语句

    分支语句格式:

    case 变量 in
    值1)
        命令序列1;;
    值2)
        命令序列2;;
    值3)
        命令序列3;;
    ....

    esac

    注意事项:
        1.case 变量 in  之间都有空格
        2.case与C中的分支语句相同, ) 就是 :




    ------------------------
         case使用例子
    ------------------------


----------------------------------------------------------------------------
  1 #!/bin/sh
  2 #This is sample about case statement
  3 if [ "$#" -ne 1 ]
  4 then
  5         echo "Your Input is erorr ! You should :./sys_case.sh number"
  6         exit 1
  7 fi
  8 case "$1" in
  9         0)      echo zero;;
 10         1)      echo one;;
 11         2)      echo two;;
 12         3)      echo three;;
 13         4)      echo four;;
 14         5)      echo five;;
 15         6)      echo six;;
 16         7)      echo seven;;
 17         8)      echo eight;;
 18         9)      echo nine;;
 19         *)      echo "INnput parameter is erorr,please Input a number"
 20 esac

        --------
          结果
        --------
    --------------------------
    [sayer@fedora shell]$ ./sys_case.sh
    Your Input is erorr ! You should :./sys_case.sh number
    [sayer@fedora shell]$ ./sys_case.sh 0
    zero
    [sayer@fedora shell]$ ./sys_case.sh 10
    Input parameter is erorr,please Input a number in 0~9

----------------------------------------------------------------------------


    -----------------------------
       JudgeNumOrChar
    -----------------------------


---------------------------------------------------------------------------------
  1 #!/bin/sh
  2 #Judge Input is num or char
  3 if [ "$#" -ne 1 ]
  4 then
  5         echo "Your input is erorr,you should :./JudgeNumOrCh.sh parameter "
  6         exit 1
  7 fi
  8
  9 case "$1" in
 10         [0-9])  echo number;;
 11        [A-Z])  echo big char;;
 12         [a-z])  echo small char;;
 13         *)      echo other char;;
 14 esac

        --------
          结果
        --------
    ---------------------------------------
    sayer@fedora shell]$ ./JudgeNumOrChar.sh
    Your input is erorr,you should :./JudgeNumOrCh.sh parameter
    [sayer@fedora shell]$ ./JudgeNumOrChar.sh 2
    number
    [sayer@fedora shell]$ ./JudgeNumOrChar.sh a
    small char
    [sayer@fedora shell]$ ./JudgeNumOrChar.sh A
    big char

    存在问题:若将line11 与 line12交换,则输入大写字母输出的是small char

----------------------------------------------------------------------------------
0 0
原创粉丝点击