if 语句-条件选择

  • if 是一个shell的关键字

    [root@Centos6app]#type if
    if is a shell keyword

    if 选择执行
    if 可以嵌套使用

  • 用法
    if COMMANDS; then 
            COMMANDS; 
    [ elif COMMANDS; then 
            COMMANDS; ]
            ... 
    [ else COMMANDS; ] 
    fi
    执行 “if COMMANDS” 列表。 如果它的退出状态是零,那么执行“然后命令”列表。 
    否则,依次执行每个“elif COMMANDS”列表,如果其退出状态为零,则执行相应的“then COMMANDS”列表,然后执行if命令。
    否则,执行“else COMMANDS”列表(如果存在)。
    整个结构的退出状态是所执行的最后一个命令的退出状态,如果条件不成立,则为零。
    if 支持单分支、双分支、多分支用法:
    if  判断条件1 ;  then
        条件为真的分支代码 
    elif 判断条件2; then
        条件为真的分支代码
    elif 判断条件3; then
        条件为真的分支代码
    else
    以上条件都为假的分支代码
    fi
    逐条件进行判断,第一次遇为“真”条件时,执行其分支,而后结束整个if语句
  • 示例
    根据命令的退出状态来执行命令
    if ping -c1 -W2 station1 &> /dev/null ; then
               echo 'Station1 is UP'
    elif grep "station1" ~/maintenance.txt &> /dev/null ; then
               echo 'Station1 is undergoing maintenance‘
    else
               echo 'Station1 is unexpectedly DOWN!' 
                             exit 1
    fi
    判断年龄----->最简单的示例
    read -p "Please input your age: " age
    if [[ ! "$age" =~ ^[0-9]+$ ]] ; then
        echo "please input right number!"
        exit 2
    elif    [ "$age" -le 18 ] ; then
        echo "you are a child"
    elif    [ "$age" -le 50 ] ; then
        echo "you are very young"
    elif    [ "$age" -le 80 ] ; then
        echo "you can go home"
    elif    [ "$age" -le 120 ] ; then
        echo "you need to feel life"
    else    
        echo "Go to hell"
    fi
    if嵌套最简单示例
    read -p "Please input your score: " score
    if [[ "$score" =~ ^[0-9]+$ ]]; then                        --------->注意书写时 变量score 上下统一,**别上下写的不统一**,就容易报错
        if      [ "$score" -le 59 ] ; then
                echo "your score is so bad"
        elif    [ "$score" -le 70 ] ; then
                echo "your score is good"
        elif    [ "$score" -le 100 ] ; then
                echo "unbeliveable"
        else
                echo "Go to hell"
        fi
    else
        echo "Please input correct score!"
    fi
  • 练习题
1、编写脚本/root/bin/createuser.sh,实现如下功能:使用一个用户名做为参数,如果指定参数的用户存在,就显示其存在,否则添加之;显示添加的用户的id号等信息
#!/bin/bash
if id $1 &> /dev/null ; then
        echo "$1 User already exists!"
else
        useradd $1 > /dev/null &&
        echo "$1 User created successfully,$1 info `id $1`"
fi
如果不用if语句,id $1 &> /dev/null && echo "$1 user already exists!" || useradd $1 ; echo "User created successfully,$1 info `id $1`"

2、编写脚本/root/bin/yesorno.sh,提示用户输入yes或no, 并判断用户输入的是yes还是no,或是其它信息
#!/bin/bash
read -p "Do you agree? (yes or no): " ans
if [[ "$ans" =~ ^[Yy]([Ee][Ss])?$ ]] ; then
        echo "Ok"
elif    [[ "$ans" =~ ^[Nn][Oo]?$ ]] ; then
        echo "hehe"
else
        echo "Input error"
fi

3、编写脚本/root/bin/filetype.sh,判断用户输入文件路径,显示其文件类型(普通,目录,链接,其它文件类型)
#!/bin/bash
read -p "Please enter the file name: " file
if [ -f "$file" ] ; then
        echo "The $file is ordinary documents"
elif    [ -d "$file" ] ; then
        echo "The $file is table of contents"
elif    [ -L "$file" ] ; then
        echo "The $file is link file"
else
        echo "The $file is othe types"
fi

4、编写脚本/root/bin/checkint.sh,判断用户输入的参数是否为正整数
#!/bin/bash
1、
read -p "Please input a number: " num
if [[ "$num" =~ ^[[:digit:]]+$ ]] ; then
        if      [[ "$num" -gt 0 ]] ; then
                echo "Entered correctly!"
        else
                echo "Input error!"
        fi
else
        echo "Not a positive integer"
fi
2、
read -p "please input :" nub
if [[ $arg =~ ^[[:digit:]]+$ ]] && [ $(expr $arg + 0) != 0 ] ; then
        echo "Bingo!"
else
        echo "Wrong!"
fi

case 语句-条件判断

[root@Centos6app]#type case
case is a shell keyword
  • 用法
    case WORD in [PATTERN [| PATTERN]...) COMMANDS ;;]... esac
    case 变量引用 in 
    PAT1)
         分支1
         ;;
    PAT2)
         分支2
         ;; 
    ...
    *)
         默认分支
         ;; 
    esac
  • case支持glob风格的通配符:
    *: 任意长度任意字符
    ?: 任意单个字符 
    []:指定范围内的任意单个字符 
    a|b: a或b
    最简单示例
    yes or no
    #!/bin/bash
    read -p "Do you agree? (yes or no): " ans
    case $ans in
    [Yy]|[Yy][Ee][Ss])
          echo "Ok"
                ;;
    [Nn]|[Nn][Oo])
        echo "hehe"
                ;;
    *)
        echo "What do you want to do !"
                ;;
    esac