1

I am using the following command in a bash script to download files from an ftp server. This command uses the -v option which shows the name of each file being downloaded.

/usr/bin/lftp ftp://$ftpuser:$ftppass@$ftphost -e "$ftpsettings ;
  mirror -v -e $remotemedia $localmedia ; quit"

I need to modify the script, to display a progress bar using whiptail, to show the download progress. This is the progress bar, but I don't know how to make it read the lftp download progress.

{
    for ((i = 0 ; i <= 100 ; i+=5)); do
        sleep 0.1
        echo $i
    done
} | whiptail --gauge "Downloading file $name..." 6 50 0

How can I do this?

1

To do a percentage you need to know the total number of files. You can get this from an ls -R inside another lftp, starting at the same directory as your mirror command. I subtracted 2 for "." and ".." in the listing.

let total=$(lftp "ftp://$ftpuser:$ftppass@$ftphost" -e "$ftpsettings ;
   ls -R $remotemedia; quit" | wc -l)-2

You can then read each line output from lftp, and for those beginning "Transferring" you can increment a line counter, calculate the percentage, and use the "XXX" input style that --gauge accepts (XXX, a percent, a title, XXX).

lftp "ftp://$ftpuser:$ftppass@$ftphost" -e "$ftpsettings ;
  mirror -v -e $remotemedia $localmedia ; quit" |

while read word word2 filename
do    if [ "$word" = Transferring ]
      then let count=count+1
           let percent=count*100/total
           echo -e "XXX\n$percent\nDownloading $filename\nXXX"
      fi
done |
whiptail --title lftp --gauge progress 10 60 0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.