Trim变量开头的某个字符

Trim a certain character from the begin of a variable

我想trim从一个变量开始的字符为零“0”。
在以下函数中,输入是 IP 地址的一部分,值为 0 到 255。

1) 当值为 1 位数字 (0-9) 时结束输入也是 1 位数字 (0-9) 那么如果它是 2 位数字 (00- 09) 或 3 位数字 (000-009) trim 1 或 2 个零“0”,因此输出将为 (0-9)。

2) 当值为 2 位数字 (10-99) 时结束输入也是 2 位数字 (10-99) 那么如果它是 3 位数字 (010- 099) trim 零 "0" 从一开始所以输出将是 (10-99).

3) 当值为 3 位数字 (100-255) 时,则不要 trim 任何内容。

function SubnetIP () {
while true; do
echo -e '\n\n\e[1;33mInput the IP Number that your Subnet want to start\e[0m\n'
echo -e '\n\e[32mInput only the third (3) section of the IP Range that you want .
(e.g. For Subnet 192.168.224.0 Input 224\e[0m\n'
echo -e '\e[38;5;208mInput a number between 0 and 255\e[0m'
echo -en '\e[95mSubnet IP : \e[0m' ; read -n3 ips ; echo
    [[ $ips =~ ^[0-9]+$ ]] || { echo -e '\n\e[38;5;208mSorry input a number between 0 and 255\e[0m\n' ; continue; }
    if ((ips >= 0 && ips <= 255)); then break ; else echo -e '\n\e[31mNumber out of Range, input a number between 0 and 255\e[0m\n' ; fi

done
echo -e '\n\nIP : '${ips}'\n\n'
}

您已经确保只接受数字 0-9,这很好。将经过验证的输入值传递给 "expr" 以去除前导零:

[[ $ips =~ ^[0-9]+$ ]] || { echo -e '\nSorry input a number ' ... etc.}
ips=$(expr ${ips})
if ((ips >= 0 && ips <= 255)); then break ; else echo -e ... etc.