直接复制粘贴就能使用的 shell 脚本函数 ①

1、输出错误 红色

echoerror()
{
    ERRORMSG=$1
    echo -e "\033[31m$ERRORMSG\033[0m"
    echo "[$(date "+%Y-%m-%d %H:%M:%S")] [ERROR] $ERRORMSG" >> /dev/null
}

 2、输出警告 黄色

echowarn()
{
    WARNMSG=$1
    echo -e "\033[33m$WARNMSG\033[0m"
    echo "[$(date "+%Y-%m-%d %H:%M:%S")] [WARN] $WARNMSG" >> /dev/null
}

3、输出提示 绿色

echotips()
{
    TIPSMSG=$1
    echo -e "\033[32m$TIPSMSG\033[0m"
    echo "[$(date "+%Y-%m-%d %H:%M:%S")] [INFO] $TIPSMSG" >> /dev/null
}

4、输入本机服务器IP地址

getlocalserverip(){ 
    isFirstInput=$1
    varri=0    
    while :
    do
        if [ $isFirstInput -eq 0 ];then
            echotips "Please input local IP address[such as 192.168.0.11]:"
        else
            echotips "Please input local IP address again[such as 192.168.0.11]:"
        fi
        read tmpserverip                    #读入服务器IP#
        if [ "$tmpserverip" = "" ]
        then
            echoerror "[Error]You must input IP address."
        else
            checklocalip "$tmpserverip"
            case "$?" in
            "0")
                LOCALSERVERIP=$tmpserverip
                break
            ;;
            "1")
                varri=`expr $varri + 1 `
                if [ "$varri" -eq 5 ]
                then
                    echoerror "[Error]You had input wrong serverip($tmpserverip) 5 times"
                    exit 1
                fi
               continue
            ;;
            esac
        fi
    done
}

5、输入服务器IP地址

getserverip(){
    INPUTIPNAME=$1
    INPUTIPKEYNAME=$2
   varri=0    
    while :
    do
        echotips "Please input ${INPUTIPNAME} server IP address[such as 192.168.0.11]:"
        read tmpserverip                    #读入服务器IP#
        if [ "$tmpserverip" = "" ]
        then
            echoerror "[Error]You must input IP address."
        else
            checkip "$tmpserverip"
            case "$?" in
            "0")
                #填入的是一个有效字符串,输入的字符串进行赋值#
                echo "$INPUTIPKEYNAME=$tmpserverip">>${TOP_PATH}/.hc.tmp
                break
            ;;

            "1")
                echoerror "[Error]You input an illegal IP address"
                varri=`expr $varri + 1 `
                if [ "$varri" -eq 5 ]
               then
                    echoerror "[Error]You had input wrong serverip($tmpserverip) 5 times"
                    exit 1
                fi
               continue
            ;;
            esac
        fi
    done
}

6、校验IP地址格式

checkip()
    inputorigin="$1"
    strLen=$(expr length $inputorigin)
    #判断IP地址的位数是否非法#
    if [ "$strLen" -gt "15" ] || [ "$strLen" -lt "7" ]    
    then
        echoerror "Wrong ip address,the length of ip address should be less than 15 and more than 7"
        return 1
    fi
    #判断IP地址是否包含非法字符#
    teststring=${inputorigin#*[!0-9.]}
    if [ "$inputorigin" != "$teststring" ]
    then
        echoerror "IP address can only contain 0-9 and dot."
        return 1
    fi
    for i in 0 1 2 3
    do
        if [ $i = 0 ]                    #判断第一个数字段是否非法#
        then
            ipval=${inputorigin%%.*}
            if [ -z "$ipval" ]
            then
                echoerror "ip0val:$ipval "
                return 1
            fi
           iptmp0=${inputorigin#*.}
            if [ "$iptmp0" = "$inputorigin" ]
            then
                echoerror "$ipval error"
                return 1
            fi
            if [ "$ipval" -le "0" ]       #判断第一个数字段是否小于等于0#
            then
                echoerror "ip0 is smaller than 0 ,$ipval error"
                return 1
            elif [ "$ipval" -eq "127" ]    #判断第一个数字段是否等于127#
            then
                echoerror "ip0 equals to 127 ,$ipval error"
                return 1
            elif [ "$ipval" -gt "223" ]    #判断第一个数字段是否大于223#
            then
                echoerror "ip0 is greater than 223 ,$ipval error"
                return 1
            elif [ `echo $ipval | grep ^0` ]
            then
                echoerror "ip0 ,$ipval error"
                return 1
            fi

        elif [ $i = 1 ]                    #判断第二个数字段是否非法#
        then
            ipval=${iptmp0%%.*}
            if [ -z "$ipval" ]
            then
                echoerror "ip1val:$ipval is null"
                return 1
            fi
            iptmp1=${iptmp0#*.}
            if [ "$iptmp1" = "$iptmp0" ]
            then
                echoerror "$ipval error"
                return 1
            fi
            if [ "$ipval" -lt "0" ]         #判断第二个数字段是否小于0#
            then
                echoerror "ip1 is smaller than 0 ,$ipval error"
                return 1
            elif [ "$ipval" -gt "255" ]      #判断第二个数字段是否大于255#
            then
                echoerror "ip1 is greater than 255 ,$ipval error"
               return 1
            elif [ `echo $ipval | grep ^0` ] && [ $(expr length $ipval) -gt 1 ]
            then
                echoerror "ip1  ,$ipval error"
                return 1      
            fi
        elif [ $i = 2 ]                       #判断第三个数字段是否非法#
        then
            ipval=${iptmp1%%.*}
            if [ -z "$ipval" ]
            then
                echoerror "ip2val:$ipval is null"
                return 1
            fi
            iptmp2=${iptmp1#*.}
            if [ "$iptmp2" = "$iptmp1" ]
            then
                echoerror "$ipval error"
                return 1
            fi
            if [ "$ipval" -lt "0" ]           #判断第三个数字段是否小于0#

            then
                echoerror "ip2 is smaller than 0 ,$ipval error"
                return 1
            elif [ "$ipval" -gt "255" ]       #判断第三个数字段是否大于255#
            then
                echoerror "ip2 is greater than 255 ,$ipval error"
                return 1
            elif [ `echo $ipval | grep ^0` ] && [ $(expr length $ipval) -gt 1 ]
            then
                echoerror "ip2  $ipval error"
                return 1
            fi
        elif [ $i = 3 ]                        #判断第四个数字段是否非法#
        then
            ipval=${iptmp2%%.*}
            if [ -z "$ipval" ]
            then
                echoerror "ip3val:$ipval is null"
                return 1
            fi
            if [ `echo $iptmp2 | grep [.]` ]    #判断第四个数字段后面是否还有.#
            then
                echoerror "wrongwrongwrong$inputorigin "
                return 1
            fi
            if [ "$ipval" -lt "0" ]            #判断第四个数字段是否小于0#
            then
                echoerror "ip3 is smaller than 0 ,$ipval error"
                return 1
            elif [ "$ipval" -gt "255" ]        #判断第四个数字段是否大于255#
            then
                echoerror "ip3 is great than 255 ,$ipval error"
                return 1
            elif [ `echo $ipval | grep ^0` ] && [ $(expr length $ipval) -gt 1 ]
            then
                echoerror "ip3  ,$ipval error"
                return 1
            fi
        fi
    done

    return 0
}

     

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值