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

The following Bourne shell script and C program can accomplish the same task: re

ID: 3741008 • Letter: T

Question

The following Bourne shell script and C program can accomplish the same task:
read an IP address from user's input and check if the input IP address is valid or not.

Fill in the blanks!


Shell script
#!/bin/bash
echo -n "Please enter the ip:"
_______A._______
echo $ip|grep _______B.______________
if [_______C._______]
then
echo "Please enter IP address following the
IP address pattern"
exit
fi

#replace the dots in ip with spaces
ip=`echo $ip | sed _______D._______
valid=1
n=1
for num in $ip
do
if [ _______E._______ ]
then
echo "INVALID: Please enter a number
between 0-255 in field $n (index starts
from 1 from left)"
valid=0
exit
fi
n=`expr $n + 1`
done
if [ $valid -eq 1 ];then
echo "The ip you entered is valid"
fi

C program

#include<stdio.h>
int main(){
int ip1=0,ip2=0,ip3=0,ip4=0;
int n=0;
n=scanf("_______A2._________",
_______B2.__________);
if (n!=4) {
printf("Please enter IP address
following the IP address pattern");
_______C2._______
}
int valid=0;
if (ip1<0 || ip1>255) valid = 1;
else if (ip2<0 || ip2>255) valid = 2;
else if (ip3<0 || ip3>255) valid = 3;
else if (_______D2._______) valid = 4;
if (_______E2._______) {
printf("INVALID: Please enter a
number between 0-255 in field %d (index
starts from 1 from left) ",valid);
}else{
printf("The ip you entered is
valid ");
}
return 0;
}

Explanation / Answer

NOTE: I have completed your assignment. Please check and let me know if you find any issues. I will revert back within 24 hours. thanks for your patience.

Q) For the shell script question i have given answers below with listing A, B etc and its answers. Please check.

A) read ip
B) -E 'd{1,3}.d{1,3}.d{1,3}.d{1,3}'
C) $? -ne 0
D) 's/.//g'
E) $num -le 255

Q) For the C programming question i have given answers below with listing A2, B2 etc and its answers. Please check.
scanf("%u.%u.%u.%u", &ip1, &ip2, &ip3, &ip4);
is the scanf statement to read the four octets of IP address into ip1, ip2, ip3 and ip4.
A2) %u.%u.%u.%u
to read four unsigned integers
B2) &ip1, &ip2, &ip3, &ip4
to read four IP integers
C2) return 0
D2) ip4 < 0 || ip4 >255
E2) ! valid