当前位置: > shell编程 >

shell运维自动化if-read(2)

时间:2014-08-31 02:11来源:linux.it.net.cn 作者:it


如果得到了输入就进行判断,如果给的是老鼠就说谢谢,如果不是老鼠就不要,并提示用户
    # vi 2.3.2.sh
    #!/bin/sh
    #code by scpman
    #cat's food
    read -p "Please input the cat's food:  " food
    if [ -n "$food" ]
    then
    echo the food:$food
    else
    echo You give cat a null
    fi
    if [ "$food" = 'laoshu'  ]
    then
    echo 3Q,cat love laoshu
    elif [ "$food" = 'pig' ]
    then
    echo sorry,cat not love pig
    else
    echo cat not like others!
    Fi
    # sh 2.3.2.sh
    Please input the cat's food:
    You give cat a null #这里为空应该提示完直接退出?
    cat not like others! #这一句怎么也输出了?
    Shell:
    exit 脚本退出
    # vi 2.3.3.sh
    #!/bin/sh
    #code by scpman
    #cat's food
    read -p "Please input the cat's food:  " food
    if [ -n "$food" ]
    then
    echo the food:$food
    else
    echo You give cat a null
    exit ####看这里,#号代表注释,这样当条件走这时,就直接提示并退出 了
    fi
    if [ "$food" = 'laoshu'  ]
    then
    echo 3Q,cat love laoshu
    elif [ "$food" = 'pig' ]
    then
    echo sorry,cat not love pig
    else
    echo cat not like others!
    Fi
    # sh 2.3.3.sh
    Please input the cat's food:
    You give cat a null
    这次就对了
    # sh 2.3.3.sh
    Please input the cat's food:  pig
    the food:pig
    sorry,cat not love pig
    scpman# sh 2.3.3.sh
    Please input the cat's food:  lkjdslfldsf
    the food:lkjdslfldsf
    cat not like others!
    scpman# sh 2.3.3.sh
    Please input the cat's food:  laoshu
    the food:laoshu
    3Q,cat love laoshu
    当然用不了,写这么多行,就能搞定,本书的所有例子,不要求有多少行,只要求思路清晰
    希望大家少写NB的代码(只有自己想几次才看懂的代码!)
    看到着,免得被大家骂,咱们赶紧来一个有用的例子吧!

(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容