Relatively new to scripting, but really struggling with something that will no doubt be second nature to most people on here:
Trying to get an exact match on $sub, where sub is an ip address.
subnet ()
{
clear
while true
do
echo "\n "
echo "Please enter the subnet to be changed: \c"
read sub
grep -w "$sub" subnet
if [[ $? -eq 0 ]]
then
print "Matched"
break;
else
echo "\n\n\tNot a valid subnet - please try again.";
fi
done
}
As a testing mechanism I've created a file called subnet, containing 111.222.333.0, but no matter what I seem to do, I can't get it to match and "only" match the exact content of subnet.
what if you have 127.0.0.18 and 127.0.0.128 in the file? You will get a match if you look for 127.0.0.1 even if it isn't in there...
grep -i is a wild way of getting some clue for something else and cant be trusted when u look for something in particular. And -i is for case insensitive search and will not hold for numerals and special characters.
Basically the subnet file will contain a list of subnets where the first to values (111.222 in this case) will not change so entries could be as follows:
111.222.xxx.x
or
111.222.xx.x
or
111.222.xxx.xx
etc etc.
So for example if the subnet was 111.222.15.0 (and making the assumption that there is also a .115 subnet present) a grep will return both:
111.222.15.0 and 111.222.115.0
What I'm struggling to do is pick out the .15.0 subnet only
subnet ()
{
clear
while true
do
echo "\n "
echo "Please enter the subnet to be changed: \c"
read sub
grep "${sub//./\\.}$"
echo $?
if [[ $? -eq 0 ]]
then
print "Arrived"
break;
else
echo "\n\n\tNot a valid subnet - please try again.";
echo $?
fi
done
}
--snip--
Please enter the subnet to be changed: 111.222.333.0
./generate-batchlist[9]: "${sub//./\\.}$": bad substitution
You have not specified the file after the grep statement...
But there is another problem here, your shell does not understand that kind of substitution. You would need to use /usr/dt/bin/dtksh (ksh93) or bash if available.