shell脚本编程-分支语句-if-case

第一,两路分支语句,if 语句:

#!/bin/bash

if [ -d $1 ]                                 :判断输入的参数是否为一个目录
then                                         :是,输出语句
        echo "$1 is s dirextory"
        exit
else                                          :嵌套 if else 语句
        if [ -f $1 ]                           :判断是否是一个普通文件
        then echo "$1 is a common file"     :是输出语句
        else
                echo "unknown"        :否则,打印“未知”
        fi
fi

 

第二,多路分支语句,case 语句:

#!/bin/bash                                           :使用bash解释器

echo -n "please input gread!!!!!!"          :提示语

read gread                                            :键盘输入参数

g=`expr $gread / 10`                             :输入参数求余赋给g

case $g in                                              
        9|10)                                              :分数在90-100之间
        echo "$g A"                                   :输出分数和等级A
        ;;                                                    :双分号“;;”表示命令序列结束
        6|7|8)                                             :分数在60-90之间
        echo "$g B"                                   :输出分数和等级B
        ;;
        1|2|3|4|5)                                        :分数在10-60之间
        echo "$g c"                                     :输出分数和等级C
esac

 

你可能感兴趣的:(linux)