The following shell script is used to check if the input IP address is invalid o
ID: 3809427 • Letter: T
Question
The following shell script is used to check if the input IP address is invalid or not. Rs = 1 while [$rs -gt 0]; do clear echo -n Please input the ip read ip #use expr to check if the input IP address follows the IP address pattern is IP = if; then replace each dot by a space in string ip ip = isvalid = 0; #loop over the words in string ip for n in; do if [-0 $n -1t 0]; then echo "ERROR: The number of the IP should not be greater less than 255 and less than 0" break; done if; then echo'----- This is a valid IP-'. Fi else echo "ERROR: The IP format you in is wrong, the format should be like 192.168.100.1 fi echo echo -n Press 0 to exit and other numbers to continue....." read rs doneExplanation / Answer
Sample Output:
Please input the ip: 192.145.23.12
-----This is a valid IP-----
Press 0 to exit and other numbers to continue....6
Please input the ip: 192.156.34.432
ERROR: The number of the IP should not be greater than 255 and less than 0
Press 0 to exit and other numbers to continue....4
Please input the ip: 192.15.45.34.32
ERROR: The IP format you input is wrong, the format should be like 192.168.100.1
Press 0 to exit and other numbers to continue....0
script:
rs=1
while [ $rs -gt 0 ]; do
clear
echo -n "Please input the ip: "
read ip
isIp=${#ip}
if [[ $ip =~ ^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$ ]]; then
ip="${ip//./ }"
isValid=0;
for n in $ip; do
if [ $n -gt 255 -o $n -lt 0 ]; then
echo "ERROR: The number of the IP should not be greater than 255 and less than 0"
isValid=1;
break;
fi
done
if [ $isValid = 0 ];then
echo "-----This is a valid IP-----"
fi
else
echo "ERROR: The IP format you input is wrong, the format should be like 192.168.100.1"
fi
echo -n "Press 0 to exit and other numbers to continue...."
read rs
done