如何将广播IP地址切入另一个变量

How to cut broadcast ip address into another variable

如何获取IP地址的字符串。 例如我输入 192.168.2.1,是否有一些方法可以将 2 和 1 剪切成其他字符串。如果不是,我的 for 循环就会有问题。

echo -n "Please enter bmc start ip to ping 192.168."
read bmcipStartaddress

echo -n "Please enter bmc end ip to ping 192.168."
read bmcipEndaddress

for((i=$bmcipStartaddress; i<=$bmcipEndaddress; i=i+2))
do
ip="192.168.$i"
echo -n "BMC IP : $ip:"

在我看来你需要两个循环,不是吗?

read -p "Please enter bmc start ip to ping 192.168." bmcipStartaddress
read -p "Please enter bmc end ip to ping 192.168." bmcipEndaddress
outerstart=${bmcipStartaddress%.*}
outerend=${bmcipEndaddress%.*}
for ((i=$outerstart; i<=$outerend; i++)); do
    if [ $i == $outerstart ]; then
        start=${bmcipStartaddress#*.}
    else
        start=0  # or 1 or 2, depending on what you want
    fi
    if [ $i == $outerend ]; then
        end=${bmcipEndaddress#*.}
    else
        end=254  # or 253 or 255, depending on what you want
    fi
    for ((j=$start; j<=$end; j=j+2)); do
        printf 'BMC IP: 192.168.%s.%s' "$i" "$j"
    done
done

如您所见,${var#*.}$var 中的第一个点开始删除,${var%.*} 从最后一个点开始删除。