What I need is, I need an if/else condition such that, if there is any output other than /etc/cron.d/maldet_daily in the grep command, an "else" condition would say "critical: more than one maldet cron found: /etc/cron.d/malcron -- /etc/cron.d/malcrondaily -- /etc/cron.d/malcronweekly " If only /etc/cron.d/maldet_daily is there in output, it should be "OK:"
As per your requirement in Post#1, you have asked that you need to print ok when it is only single entry in cron , the code which is provided by you will not satisfy that condition, also I have added a condition if no entry found for maldet then it will show that. Could you please try code which I provided you in Post#6, let me know if that helps you.
The following seems to provide something closer to the spirit of what was requested in the original post in this thread (and, except for the one call to grep , only uses shell built-ins):
list="$(grep -rl 'maldet' /etc/cron*)"
if [ "$list" = "/etc/cron.d/maldet_daily" ]
then echo 'OK:'
else if [ "$list" = '' ]
then echo 'critical: maldet not found:'
else printf 'critical: maldet cron found in the following file(s): '
last=''
echo "$list" | while read line
do if [ "$last" != '' ]
then printf '%s -- ' "$last"
fi
last="$line"
done
printf '%s\n' "$last"
fi
fi