title: shell script in bash to download file from ftp server
body: <p>i have to write a shell script for bash shell to transfer file from ftp server 
  given<br>
  ftp server --    fileserver@example.com<br>
  user user1<br>
  password pass1</p>
  
  <p>now in /dir1/dir2 at ftp server i have folder in following forms<br>
  0.7.1.70<br>
  0.7.1.71<br>
  0.7.1.72   </p>
  
  <p>i have to copy file "file1.iso" from the latest folder i.e 0.7.1.72 in this case.
  i have to also check integrity of the file while copying i.e suppose the file are being uploaded to the server and at that time if i start copying in this case copying will not be complete.</p>
  
  <p>i have to do it after every 4 hour . this can be done by making it a cron job.  please help</p>
  
  <p>i have done this 
   i mounted the ftp server folder to my local machine . for checking if the file has been completely uploaded or not i am checking the size after every 50 sec for 5 times if it is same then i am copying it otherwise run the script after 4 hr...
  i have maintained a text file " foldernames.txt" which have name of all those folders from which i have copied the required file .. so i am checking if a new folder is added at server by checking its name in the foldername.text file ..
  **</p>
  
  <p><strong>every thing is working fine only problem now is .. suppose file was being downloaded an at that time there was some network failure.. how will i make sure that i have completely downloaded the file .... i tried to use md5sum and chksum but it was taking to long to compute on mounted folder. please help</strong></p>
  
  <p>here is my script ..</p>
  
  <pre><code>#!/bin/bash
  #
  # changing the directory to source location 
  echo " ########### " &gt;&gt; /tempdir/pvmscript/scriptlog.log
  echo `date`&gt;&gt; /tempdir/pvmscript/scriptlog.log
  echo " script is strting " &gt;&gt; /tempdir/pvmscript/scriptlog.log
  cd /var/mountpt/pvm-vmware
  #
  # array to hold the name of last five folders of the source location
  declare -a arr
  i=0
  for folder in `ls -1 | tail -5 `; do
  arr[i]=$folder
  #echo $folder
  i=$((i+1))
  done
  echo " array initialised " &gt;&gt; /tempdir/pvmscript/scriptlog.log
  #
  #now for these 5 folders we will check if their name is present in the list of copied         
  #  folder names
  #
  echo " checking for the folder name in list " &gt;&gt; /tempdir/pvmscript/scriptlog.log
  ## $(seq $((i-1)) -1 0 
  for j in $(seq $((i-1)) -1 0  ) ; do
  var3=${arr[$j]}
  #var4=${var3//./}
  echo " ----------------------------------------" &gt;&gt;  /tempdir/pvmscript/scriptlog.log
  echo " the folder name is $var3" &gt;&gt; /tempdir/pvmscript/scriptlog.log
  #
  # checking if the folder name is present in the stored list of folder names or not
  #
  #
  foldercheck=$(grep $var3 /tempdir/pvmscript/foldernames.txt | wc -l)
  #
  if test $foldercheck -eq 1
  then 
  echo " the folder $var3 is present in the list so will not copy it " &gt;&gt;  /tempdir/pvmscript/scriptlog.log
  foldercheck=" "
  continue
  else
  #
  echo " folder $var3 is not present in the list so checking if it has the debug.iso file "&gt;&gt; /tempdir/pvmscript/scriptlog.log
  #enter inside  the new folder in source
  #
  cd  /var/mountpt/pvm-vmware/$var3
  #
  # writing the names of content of folder to a temporary text file
  #
  ls -1 &gt; /var/temporary.txt
  #checking if the debug.iso is present in the given folder
  var5=$(grep debug.iso /var/temporary.txt | wc -l)
  var6=$(grep debug.iso //var/temporary.txt)
  #
  check1="true"
  #
  # if the file is present then checking if it is completely uploaded or not  
  #
  rm -f /var/temporary.txt
  if test $var5 -eq 1 
  then 
  echo " it has the debug.iso checking if upload is complete   "&gt;&gt;/tempdir/pvmscript/scriptlog.log
  #
  # getting the size of the file we are checking if size of the file is constant or     changing    # after regular interval
  #
  var7=$(du -s ./$var6 |cut -f 1 -d '.')
  #echo " size of the file is $var7"
  sleep 50s
  #
  # checking for 5 times at a regular interval of 50 sec if size changing or not 
  #
  #
  for x in 1 2 3 4 5 ;do
  var8=$(du -s ./$var6 |cut -f 1 -d '.')
  #
  #if size is changing exit and check it after 4 hrs when the script will rerun
  #echo " size of the file $x is $var7"
  if test $var7 -ne $var8
  then
  check1="false"
  echo " file is still in the prossess of being uploadig so exiting will check after 4 hr  " &gt;&gt; /tempdir/pvmscript/scriptlog.log
  break
  fi
  sleep 50s
  done
  #
  #if the size was constant copy the file to destination
  #
  if test $check1 = "true" 
  then
  echo " upload was complete so copying the debug.iso file  " &gt;&gt;  /tempdir/pvmscript/scriptlog.log
  cp $var6 /tempdir/PVM_Builds/ 
  echo " writing the folder name to the list of folders which we have copied " &gt;&gt;  /tempdir/pvmscript/scriptlog.log
  echo $var3 &gt;&gt; /tempdir/pvmscript/foldernames.txt
  echo " copying is complete  " &gt;&gt; /tempdir/pvmscript/scriptlog.log
  fi
  #else 
  #echo $foldercheck &gt;&gt; /vmfs/volumes/Storage1/PVM_Builds/foldernames.txt
  else
  echo " it do not have the debug.iso file so leaving the directory "  &gt;&gt;/tempdir/pvmscript/scriptlog.log
  echo $var3 &gt;&gt; /tempdir/pvmscript/foldernames.txt
  echo 
  fi
  #rm -f /var/temporary.txt
  fi
  done
  </code></pre>