如何获取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:"
答案 0 :(得分:1)
我觉得你需要两个循环,不是吗?
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%.*}
从最后一个点移除到最后一个点。