Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The following shell script is used to check if the input IP address is invalid o

ID: 3810277 • 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 = A. if [B. 0]; then # replace each dot by a space in string ip ip = C. isValid=0 # loop over the words in string ip for n in D.;do if [E. -o $n -1t 0]; then echo "ERROR: The number of the IP should not be greater than 255 and less than 0" F. break; fi done if [G.]; 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 echo -n "Press 0 to exit and other numbers to continue..." read rs done

Explanation / Answer

#!/bin/bash

rs=1
while [ $rs -gt 0 ]; do

clear
echo -n "Please input the ip: "
read ip

isIP=`expr match $ip '[0-9]+.[0-9]+.[0-9]+.[0-9]+'`;

if [ $isIP -ne 0 ]; then
ip=`echo $ip| sed 's/./ /g'`
  
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 -eq 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
echo -n "Press 0 to exit and other number to continue..."
read rs

done

  

Output:

Please input the ip: 255.255.255.255
-------This is a valid IP--------

Press 0 to exit and other number to continue...9

Please input the ip: 120.222.256.4
ERROR: The number of the IP should not be greater than 255 and less than 0

Press 0 to exit and other number to continue...0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote