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
cmp
anddiff
, but I'm not sure that is possible without a temporary file or pipe to hold the data fromdiff
until you determine ifmailx
should run.diff
will 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 thewhile
loop) to the compound command that runsmailx
andcp
. Technically, bothmailx
andcp
can both read from the pipe, butmailx
will exhaust all the data beforecp
runs, and thiscp
command would ignore its standard input anyway.