Home  >  Article  >  Database  >  bash一些技巧

bash一些技巧

WBOY
WBOYOriginal
2016-06-07 15:41:301020browse

1. 设置当命令带有非0返回时立刻退出:set -e 2. 十六进制转换为十进制:num_16=`echo ibase=10;obase=16;$num|bc` 3. IP合法性校验 function valid_ip() { local ip_local=$1 local stat=1 if [[ $ip_local =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{

1. 设置当命令带有非0返回值时立刻退出:set -e

2. 十六进制转换为十进制:num_16=`echo "ibase=10;obase=16;$num"|bc`

3. IP合法性校验

function valid_ip()

{

    local  ip_local=$1

    local  stat=1

    if [[ $ip_local =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then

        OIFS=$IFS

        IFS='.'

        ip_local=($ip_local)

        IFS=$OIFS

if [[ -z ${ip_local[0]} ]] || [[ -z ${ip_local[1]} ]] || [[ -z ${ip_local[2]} ]] || [[ -z ${ip_local[3]} ]]

then

return 1

fi

        [[ ${ip_local[0]} -le 255 && ${ip_local[1]} -le 255 \

            && ${ip_local[2]} -le 255 && ${ip_local[3]} -le 255 ]]

        stat=$?

    fi

    return $stat

}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn