zoukankan      html  css  js  c++  java
  • linux 条件判断式

    1.利用if ...then

    if [ 判断条件 ];then
        指令
    fi

    实例一 Y/N:

    #!/bin/bash
    #Program:
    #       This program shows "Hello World!" in your screen.
    #History:
    #       2017/07/31      lzyer  First release
    read -p "Please input (Y/N)? " yn
    if [ "${yn}" == "Y" ] || [ "${yn}" == "y" ];
    then
            echo "OK,continue."
            exit 0
    fi
    if [ "${yn}" == "N"] || [ "${yn}" == "n" ];
    then
            echo "oh,interrupt!"
            exit 0
    fi
    echo "I don't know what your choice is " && exit 0

    实例二:

    1. 判断 $1 是否为 hello,如果是的话,就显示 "Hello, how are you ?";
    2. 如果没有加任何参数,就提示使用者必须要使用的参数下达法;
    3. 而如果加入的参数不是 hello ,就提醒使用者仅能使用 hello 为参数。
    #!/bin/bash
    #Program:
    #       check $1 is equal to "hello"
    #History:
    #2017/08/04     lzyer   First Release
    if [ "${1}" == "hello" ];then
            echo "Hi, how are you?"
    elif [ "${1}" == "" ]; then
            echo "you must input parameter"
    else
            echo "hello~~~"
    fi

    实例三 使用netstat 的指令查看服务的状态

    #!/bin/bash
    #Program:
    #       using netstat and grep to detect www.ssh,ftp and mail service.
    #History:
    #2017/08/05     lzyer  First release
    echo "Now, I will detect your Linux server' service!"
    echo -e "The www,ftp,ssh and mail will be detect!"
    testfile=/home/lzyer/test/bin/netstat_file.txt
    netstat -tuln > ${testfile}
    testing=$(grep ":80" ${testfile})
    if [ "${testing}" != "" ];then
            echo "www is running in your system."
    fi
    testing=$(grep ":22" ${testfile})
    if [ "${testing}" != "" ];then
            echo "ssh is running in your system."
    fi
    testing=$(grep ":21" ${testfile})
    if [ "${testing}" != "" ];then
            echo "ftp is running in your system."
    fi
    testing=$(grep ":25" ${testfile})
    if [ "${testing}" != "" ];then
            echo "mail is running in your system."
    fi

    实例四:计算退伍时间

    #!/bin/bash
    #Program:
    #       You input demobillization date, I calculate how many days before you demobilize.
    #History:
    #2017/08/05     lzyer   First release
    echo "this program will try to calculate:"
    read -p "how many days before you demobillization date (YYYYMMDD>20170805): " date2
    date_d=$(echo ${date2} | grep '[0-9]{8}')
    if [ "${date_d}" == "" ];then
            echo "You input the wrong date format..."
            exit 1
    fi
    declare -i date_dem=$(date --date="${date2}" +%s)
    declare -i date_now=$(date +%s)
    declare -i date_total_s=$((${date_dem}-${date_now}))
    declare -i date_d=$((${date_total_s}/60/60/24))
    if [ ${date_total_s} -lt 0 ];then
            echo "You had been demobilization before : " $((-1*${date_d})) "day ago"
    else
            declare -i date_h=$((((${date_total_s}-${date_d}*60*60))/60/60))
            echo "You will demobilization after ${date_d} days and ${date_h} hours."
    fi

    2.利用 case ..... esac 判断

    实例一:

    #!/bin/bash
    #Program:
    #       show "hello" from $1...by case esac
    #History:
    #2017/08/05     lzyer   first release
    case ${1} in
            "hello")
            echo "hello,how are you?";;
            "")
            echo "please your input sth.";;
            *)
            echo "Usage ${0} hello";;
    esac

    3.利用 function 功能

    #!/bin/bash
    #Program:
    #       function use
    #History:
    #2017/08/05     lzyer  first release
    function print(){
            echo  "Your choice is ${1}"
    }
    echo "This program will print your selection !"
    case ${1} in
            "one") print 1;;
            "two") print 2;;
            "three") print 3;;
            *) echo "Usage ${0} one|two|three";;
    esac
  • 相关阅读:
    正则表达式学习
    《代码整洁之道》阅读笔记
    PHP手册阅读笔记(一)——XXX
    2014年终总结和2015年规划
    linux之帮助命令——help,man,whereis简介
    企业中git管理代码的基本流程
    推荐几款画韦恩图的在线工具
    HTTPContent-Type的含义
    s s
    asp.net core ServiceProvider
  • 原文地址:https://www.cnblogs.com/lzeffort/p/7291909.html
Copyright © 2011-2022 走看看