Следующий код работает для получения значения состояния a
, если он включен, d
, если он отключен, и n
, если его нет в файле, указанном ниже:
# 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`
Из следующего файла:
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
Примечание: $1
= столбец 1 в файле и $2
= столбец 3 в файле.
Так что меня беспокоит, достаточно ли хороша вышеуказанная функция поиска в следующем формате? или есть еще какое-нибудь лучшее регулярное выражение:
format=sprintf("^[\t ]*%s.*%s",serv,proto);
comformat=sprintf("^[\t ]*#[\t ]*%s.*%s",serv,proto);