I am creating bash script on a file to send diff over the mail.
for below case, I have created two files as "xyz.conf" and "xyz.conf_bkp" to compare
So far, I have come with below script -
file="/a/b/c/xyz.conf"
while true
do
sleep 1
cmp -s $file ${file}_bkp
if [ $? > 0 ]; then
diff $file ${file}_bkp > compare.log
mailx -s "file compare" [email protected] < compare.log
sleep 2
cp $file ${file}_bkp
exit
fi
done
I have scheduled above script to run every second
* * * * 0-5 script.sh
This is working fine but I am looking with different approach like below - I am looking for to work without creating another backup file Imagine if I have to work multiple files which will lead me to crate those many backups which doesn't look good solution.
Can anyone suggest, how to implement this approach?
I would write it this way.
I wanted to avoid running both
cmpanddiff, but I'm not sure that is possible without a temporary file or pipe to hold the data fromdiffuntil you determine ifmailxshould run.diffwill produce no output when its exit status is 0, so when it finally has a non-zero exit status its output is piped (as the output of thewhileloop) to the compound command that runsmailxandcp. Technically, bothmailxandcpcan both read from the pipe, butmailxwill exhaust all the data beforecpruns, and thiscpcommand would ignore its standard input anyway.