直接复制粘贴就能使用的 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
}

     

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Shell脚本中,函数是一种用于封装一段可重复使用的代码块的方式。通过定义函数,可以将一系列命令或操作组织起来,使得代码更加模块化和易于维护。 要在Shell脚本使用函数,可以按照以下步骤进行操作: 步骤1: 定义函数脚本使用`function`关键字或直接使用函数名来定义函数函数定义的一般格式如下: ``` function 函数名() { # 函数体,包含一系列命令或操作 } ``` 举例来说,我们可以定义一个名为`hello`的函数,用于输出"Hello, World!"的消息: ``` function hello() { echo "Hello, World!" } ``` 步骤2: 调用函数 在需要使用函数的地方,可以通过函数名后跟一对圆括号来调用函数。例如,要调用上述定义的`hello`函数,可以使用以下语法: ``` hello ``` 步骤3: 函数参数 函数还可以接受参数,以便在函数体内使用。参数通过在函数名后的圆括号内指定,并可以在函数体内通过`$1`、`$2`等变量名来引用这些参数的值。例如,定义一个名为`add`的函数,用于计算两个数的和: ``` function add() { sum=$(($1 + $2)) echo "The sum is $sum" } ``` 通过在函数名后的圆括号内提供参数值,可以调用函数并传递参数。例如,要计算1和2的和,可以使用以下语法: ``` add 1 2 ``` 以上就是在Shell脚本使用函数的基本步骤。通过定义函数,可以将一段重复使用的代码封装起来,使得代码更加清晰、模块化和易于维护。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Shell脚本使用function(函数)示例](https://download.csdn.net/download/weixin_38734506/12846564)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [shell脚本函数——【初学者必看】](https://blog.csdn.net/weixin_59663288/article/details/124478809)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Shell脚本攻略:shell函数应用](https://blog.csdn.net/cronaldo91/article/details/130974593)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值