The following code works to get the status value of a if its enabled, d if its disabled and n if its not present from a file given below:
# Check whether the service entry exists and whether it is enabled or disabled
# Status value 'a' states that the service is uncommented in /etc/inetd.conf.
# Value 'd' states that the service is commented, and value 'n' specifies
# that the service entry doesnt exist in the configuration file.
status=`awk -v serv=$1 -v proto=$2 -v exist="n" '
BEGIN {
format=sprintf("^[\t ]*%s.*%s",serv,proto);
comformat=sprintf("^[\t ]*#[\t ]*%s.*%s",serv,proto);
}
{
if(match($0,format))
{
exist="a";
}
else if(match($0,comformat))
{
exist="d";
}
}
END {
printf("%s",exist)
}' $INETD`
From the following file:
ftp stream tcp6 nowait root /usr/sbin/ftpd ftpd
telnet stream tcp6 nowait root /usr/sbin/telnetd telnetd -a
shell stream tcp6 nowait root /usr/sbin/rshd rshd
#kshell stream tcp nowait root /usr/sbin/krshd krshd
login stream tcp6 nowait root /usr/sbin/rlogind rlogind
#klogin stream tcp nowait root /usr/sbin/krlogind krlogind
Note: $1 = column 1 in the file and $2 = column 3 in file.
So my concern is if the above searching using the following format good enough? or is there anyother better regular expression:
format=sprintf("^[\t ]*%s.*%s",serv,proto);
comformat=sprintf("^[\t ]*#[\t ]*%s.*%s",serv,proto);
Based on what I have understood of the problem this might work –
Test: Based on passing the values manually and using the below content in file