Within the script, I’ve been using wc (word count) to check files after making changes to them. It works, but I was wondering if there’s a better way of checking the file. For example, If I used wc -m and accidentally put in a blank line in the script. The script will fail unless I catch it and change the expected output of wc -m.
cat <<-EOF > /etc/hosts
192.168.1.1
192.168.1.2
192.168.1.3
EOF
if [ "$(wc -m /etc/hosts" == "33 /etc/hosts" ] ; then
echo it worked
else
echo it didnt work
fi
There is not going to be any universally good verification. Your checks will be specific to what you think the file should look like and how you think the file should have changed. One thing I would suggest is to back up the file before you change it so that you can do relative checks instead of absolute checks.