Skip to content

Instantly share code, notes, and snippets.

@csuter
Created June 30, 2010 21:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save csuter/459270 to your computer and use it in GitHub Desktop.
Save csuter/459270 to your computer and use it in GitHub Desktop.
#!/bin/bash
reliably_download()
{
ssh_user=$1
remote_host=$2
remote_file=$3
local_file=$4
# default to 3 tries
max_tries=${5-3}
limit=$((max_tries+1))
local_file_md5=''
remote_file_md5=`ssh -n ${ssh_user}@${remote_host} "md5sum ${remote_file} | awk '{print \\\$1}'"`
ntries=1
while [ \( "${local_file_md5}" != "${remote_file_md5}" \) -a \( "${ntries}" \< "${limit}" \) ]
do
echo "downloading ${host}:${remote_file} - attempt ${ntries}"
scp ${ssh_user}@${remote_host}:${remote_file} ${local_file} >/dev/null
local_file_md5=`md5sum ${local_file} | awk '{print $1}'`
((ntries += 1))
done
if [ "${local_file_md5}" == "${remote_file_md5}" ]
then
return 0
else
return 1
fi
}
echo BEFORE
ls -l /tmp/test.xml
echo
reliably_download "hadoop" "chris.in.escapemg.com" "/tmp/original/test.xml" "/tmp/test.xml" 5
echo
echo AFTER
ls -l /tmp/test.xml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment