1753797 Members
8474 Online
108805 Solutions
New Discussion юеВ

Re: Help ksh script

 
SOLVED
Go to solution
zap_2
Advisor

Help ksh script

Dear All,

Terms:
OFA = Original File on Server A
TFA = Target File on Server A
TFB = Target FIle on Server B
I want to create script that do the following steps:
1)copy file on server A from OFA to TFA
2)check using md5sum OFA and TFA
if (md5sum OFA = md5sum TFA) then
gzip TFA
md5sum TFA.gz
else go to step 1)
3)rcp -p serverA:TFA.gz serverB:TFB.gz
md5sum TFB.gz
if (md5sum TFA.gz = md5sum TFB.gz) then
remsh B gunzip TFB.gz
remsh B chmod 444 TFB
else go to 3)

Please help.
Many thanks.
10 REPLIES 10
Dennis Handly
Acclaimed Contributor
Solution

Re: Help ksh script

Something like this. Note it is a waste of time to use md5sum to compare two local files, when you could use diff.

#1)copy file on server A from OFA to TFA

while : ; do
cp -f $OFA $TFA

#2)check using md5sum OFA and TFA
#This is a waste of time
diff $OFA $TFA > /dev/null
if [ $? -eq 0 ]; then
rm -f $TFA.gz
gzip $TFA
MD5=$(md5sum $TFA.gz | awk '{print $1}')
break
fi
done

#3)rcp -p serverA:TFA.gz serverB:TFB.gz
while : ; do
remsh $serverB -n rm -f $TFB.gz
rcp -p $TFA.gz $serverB:$TFB.gz

MD5B=$(remsh $serverB -n md5sum $TFB.gz | awk '{print $1}')
# if (md5sum TFA.gz = md5sum TFB.gz) then
if [ "$MD5" = "$MD5B" ]; then
remsh $serverB -n rm -f $TFB
remsh $serverB -n gunzip $TFB.gz
remsh $serverB -n chmod 444 $TFB
break
fi
done
zap_2
Advisor

Re: Help ksh script

Dear Dennis,

Many thanks for your help.

Dear All,

Any another suggested script?
Many thanks in advanced.
Hakki Aydin Ucar
Honored Contributor

Re: Help ksh script

solution is good, maybe you can add file check to make more robust script steps, for example:

#2)check using md5sum OFA and TFA
#This is a waste of time

if [ -f "$TFA" ] ; then

diff $OFA $TFA > /dev/null
if [ $? -eq 0 ]; then
rm -f $TFA.gz
gzip $TFA
MD5=$(md5sum $TFA.gz | awk '{print $1}')
break
fi
fi

done
zap_2
Advisor

Re: Help ksh script

Dear Hakki thanks for your help.

Dear All please more suggested scripts.
Many thanks in advanced.
Doug O'Leary
Honored Contributor

Re: Help ksh script

Hey;

The reason he wants to do the md5sum is to verify the remote copy was completed successfully. Copying the file locally, then rcp'ing it doesn't satisfy that concern.

I *much* prefer ssh/scp to remsh and rcp. All the convenience plus security. You effectively have your script from your description. The requirements were quite specific and easily translated into ksh:

lm=1
om=2

while [ "${lm}" != "${om}" ]
do
scp -q $OFA ${target_host}:${TFA}
lm=$(md5sum $OFA}
om=$(ssh ${target_host} md5sum ${TFA})
done

## Note: if you *actually* want to copy from the second server to a third server, you will need to use ssh/scp. remsh/rcp doesn't support that concept.

## The only reason I can see to do that is if the local host and the 2nd remote host don't have direct connectivity. Otherwise, it's simpler just to reiterate the loop above using the 2nd host as target_host.

scp -q ${target_host}:$TFA ${remote_host}:$TFB
lm=1; om=2
while [ "${lm}" != "${om}" ]
do
lm=$(ssh ${target_host} md5sum ${TFA})
om=$(ssh ${remote_host} md5sum ${TFB})
done

HTH;

Doug

------
Senior UNIX Admin
O'Leary Computers Inc
linkedin: http://www.linkedin.com/dkoleary
Resume: http://www.olearycomputers.com/resume.html
Dennis Handly
Acclaimed Contributor

Re: Help ksh script

One improvement you can do is only do the while loops a fixed number of times:
(( limit = 5 ))
while (( (limit -= 1) >= 0 )); do
zap_2
Advisor

Re: Help ksh script

Dear All,

More improvements in script are welcome.
Many thanks in advanced.
Peter Nikitka
Honored Contributor

Re: Help ksh script

Hi,

BTW, Dennis:
To check two local files for equalness, I prefer
if cmp -s file1 file2
then print equal files
else print file differ
fi

The 'cmp' stops at the first difference - a 'diff' runs through the files completely.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Viktor Balogh
Honored Contributor

Re: Help ksh script

You could just simply gzip the file on Server A, that way you can save some bandwith at step 1. ;)
****
Unix operates with beer.