Operating System - HP-UX
1846256 Members
5732 Online
110256 Solutions
New Discussion

Re: Help me complete this script

 
SOLVED
Go to solution
Terrence
Regular Advisor

Help me complete this script

I need a test statement for a script. I have a file that I copy to an archive location. To ensure it's finished being created in it's original location I want to compare the original to the copy after 15 minutes and see if they are the same.

cp STOCK_FILE /var/STOCK_ARCHIVE/STOCK_FILE

sleep 900

until [ diff STOCK_FILE /var/STOCK_ARCHIVE/STOCK_FILE =0 ]
cp STOCK_FILE /var/STOCK_ARCHIVE/STOCK_FILE
sleep 300

I don't know how to do the test statement so that it checks that the diff comparison returns no difference. If there is a difference then the original file should overwrite the copy, wait 5 minutes and then try again to see if the file is done.
5 REPLIES 5
RAC_1
Honored Contributor

Re: Help me complete this script

size_of_source=`ll file|awk '{print $5}`
size_of_desti=` ll \dest\file|awk '{print $5}`

if [ ${size_of_source} -eq ${size_of_desti} ];then
echo "File copy OK"
exit 0
else
sleep sometime
cp source_file dest_file
fi

Anil
There is no substitute to HARDWORK
A. Clay Stephenson
Acclaimed Contributor

Re: Help me complete this script

You can really take advantage of this feature of diff. It returns 0 if there are no differences, 1 if there are differences, and > 1 on error. Note: We are not concerned about the output (stdout or stderr) of diff but rather the result returned to the shell.


This should make a reasonable test for you:

diff file1 file2 >/dev/null 2>&1
STAT=${?}
if [[ ${STAT} -eq 0 ]]
then
echo "Files the same"
else
echo "They ain't"
fi

You could also use the output of the cksum command for each file and compare them.
If it ain't broke, I can fix that.
KapilRaj
Honored Contributor

Re: Help me complete this script

cp will come out only after finishing the copy and if there are any errors occured you can trap it by checking $? (return-code).

cp bla /archiv/bla
if [ $? -ne 0 ]
then
echo Copy failed ...
fi

Kaps
Nothing is impossible
Dan Martin_1
Frequent Advisor
Solution

Re: Help me complete this script

until cmp -s STOCL_FILE /var/STOCK_ARCHIVE/STOCK_FILE
do
cp ....
sleep ...
done
Terrence
Regular Advisor

Re: Help me complete this script

Thank you Dan,

While the other two answers were usable, I wasn't looking for if statements, I needed it to loop until they matched hence the use of until.

Also Dan's answer was exactly what I needed and only required the one line. Simplicity is always preferable.

Thanks to all who answered.