Display download progress

Occasionally I want to display a user-friendly progress bar when doing something in a script, usually downloading a file.

This can be done by extracting the download progress as a percentage from the output of wget and passing it to dialog or zenity. As we need to work out which one to use I’ve chosen to test to see if $TERM is defined and if it is I assume we are running in a terminal on a graphical desktop – this isn’t fool-proof but seems to work in most cases.

Note – The script does NOT check for any prerequsites.

#!/bin/sh
#
# sh-download.sh
#
# This  program  is free software: you can redistribute it and/or modify  it
# under the terms of the GNU General Public License as published by the Free
# Software  Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# This  program  is  distributed  in the hope that it will  be  useful,  but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public  License
# for more details.
#
# You  should  have received a copy of the GNU General Public License  along
# with this program. If not, see <http://www.gnu.org/licenses/>
#
#  12 Apr 17   0.1   - Initial version - MEJT 
#  19 Apr 17   0.2   - Reverse output before extracting percent - MEJT
#  20 Apr 17   0.3   - Fixed problems with file names that don't have a file
#                      extension - MEJT
#                    - Doesn't generate a log file - MEJT
#
_url="http://cdimage.debian.org/debian-cd/current/amd64/iso-cd/MD5SUMS"
# OR
_url="http://cdimage.debian.org/debian-cd/current/amd64/iso-cd/"
_file="debian-8.7.1-amd64-netinst.iso"

_url="$_url$_file"
_ext="${_url##*.}"

if [ -n "$_ext" ]; then # No file extension
  _file="$(basename "$_url")"
else
  _file="$(basename "$_url" "$_ext")$_ext"
fi

_options=" -O $_file -c -e robots=off -np --progress=dot"
_text="Downloading: $_file"

if [ ! -z "$_file" ]; then
  if  [ ! -z "$TERM" ] &&  type zenity >/dev/null 2>&1 ;then
    wget $_options $_url 2>&1 \
      | stdbuf -oL sed -e 's\nothing to do\100%\g' \
      | stdbuf -oL grep --line-buffered "%" \
      | stdbuf -oL cut -f1 -d'%' | stdbuf -oL rev \
      | stdbuf -oL cut -f1 -d' ' | stdbuf -oL rev \
      | zenity --progress --title="" --text="$_text" 2>/dev/null
  else
    wget $_options $_url 2>&1 \
      | stdbuf -oL sed -e 's\nothing to do\100%\g' \
      | stdbuf -oL grep --line-buffered "%" \
      | stdbuf -oL cut -f1 -d'%' | stdbuf -oL rev \
      | stdbuf -oL cut -f1 -d' ' | stdbuf -oL rev \
      | dialog --guage "$_text" 0 100
  fi
fi
#

Taking the output and reversing it may seem an odd way to extract the percentage, but it is a neat way to get the last field in a record (and necessary here as extracting the nth field from the output of wget is difficult as there may or may not be one or more leading spaces on each line, something I didn’t notice when testing this script – which is why I had to update this post).

This script has been tested using on both Debian (jessie) and on Windows using Cygwin and although there are differences in behaviour between these two environments it works on both.

References

Advertisements
This entry was posted in Linux and tagged . Bookmark the permalink.