Shell Script to check dhcp conf file

Hi,

I have to prepare a script to check the dhcp conf file. The script has to check for a specific parameter called circuit ID. If the Circuit ID is unique it should show the output that it is unique and if it is duplicate it should show that the Circuit ID is duplicate. I have prepared the below script but every time i enter a unique Circuit ID it still shows that it is duplicate. Please, help

#!/bin/bash

echo -e "Please enter the Circuit ID : \c"
read CID
CMD="find . -type f -exec grep -H "'$CID'" {} \;"
CNT="$CMD | wc -l"
eval $CNT
read Z


if [ "$Z" != 0 ] ;
then
echo " the Circuit ID : $CID is duplicate "
echo " Please find below the list where the Circuit ID is present :"
eval $CMD
exit 0
else
echo " the Circuit ID : $CID  is unique. "
fi

I'm not sure how the script should fit the verbal description you have given.

While you say you want to check one single file, you run a find across ALL files (no matter what their contents/meaning might be) in the current directory and all subdirectories grep ping for the circuit ID.
Then you read Z - from stdin! Nothing to do with your results so far.
And, why should anything be unique when its count is zero?

Dear Rudi,

The script in placed in /etc/dhcp directory, so the find commandin expected to read the dhcp conf file. the Count is taken in case multiple entries of the same Circuit ID are found.

I get that the variable "$Z" is not gtting me anywhere. Please suggest something

Thanks
Nix

How about

 [ $(grep -c "$CID" dhcp.conf) -gt 1 ] && echo duplicate || echo unique

This does not cover the case that CID is not found at all.

1 Like

Dear Rudi,

Thanks, a lot for all the help. I appended the script and got the desired result

#!/bin/bash


echo -e "Please enter the Circuit ID : \c"
read CID
[ $(grep -c "$CID" dhcp.conf) != 0 ] && echo " the Circuit ID : $CID is duplicate " || echo " the Circuit ID : $CID  is unique. "
echo -e "Please enter Subnet mask: \c"
read SB
if [ "$SB" != 255.255.224.0 ]
then
echo " The Subnet Mask is not /19 as per requirement. Please correct it "
else
echo " The Subnet Mask is /19 which is required "
fi