I'm attempting to write a bash script to check multiple files for a string, then if the string is found to remove it. Here is what I have, which I thought would work, but is only partly. The entries in the files are being removed, but running this a second time, I'm not getting the "nothing to do..." message.
#!/bin/bash
files=(
'/etc/rsyslog.conf'
'/etc/rsyslog.d/remote.conf'
'/etc/rsyslog.d/01-remote.conf'
)
tmpcheck="for f in ${files[*]}; do cat $f | grep blah | wc -l; done"
#for f in ${files[*]}; do cat $f | grep collector.acuity.com | wc -l; done
if [[ "$tmpcheck" != 1 ]];then
for f in "${files[@]}";do
echo -e "Removing blah from $f"
sed -i "/blah/d" "$f"
done
echo -e "Restarting rsyslog service"
systemctl restart rsyslog.service
else
echo -e "Nothing to do, blah has been removed from $f"
fi
Any help would greatly help.