Blog de Ubuntu en Español Blog [no oficial] de Ubuntu en Español

9Dec/100

Genial Script para descarga de ficheros con barra de progreso (wget y Zenity)

 

Un muy útil código que podemos utilizar en nuestros scripts cuando queremos descargar un fichero y mostrar una barra de progreso del mismo (requiere zenity instalado en la máquina).

DOWNLOAD() {
  rand="$RANDOM `date`"
  pipe="/tmp/pipe.`echo '$rand' | md5sum | tr -d ' -'`"
  mkfifo $pipe
  wget -c $1 2>&1 | while read data;do
    if [ "`echo $data | grep '^Length:'`" ]; then
      total_size=`echo $data | grep "^Length:" | sed 's/.*\((.*)\).*/\1/' |  tr -d '()'`
    fi
    if [ "`echo $data | grep '[0-9]*%' `" ];then
      percent=`echo $data | grep -o "[0-9]*%" | tr -d '%'`
      current=`echo $data | grep "[0-9]*%" | sed 's/\([0-9BKMG.]\+\).*/\1/' `
      speed=`echo $data | grep "[0-9]*%" | sed 's/.*\(% [0-9BKMG.]\+\).*/\1/' | tr -d ' %'`
      remain=`echo $data | grep -o "[0-9A-Za-z]*$" `
      echo $percent
      echo "#Downloading $1\n$current of $total_size ($percent%)\nSpeed : $speed/Sec\nEstimated time : $remain"
    fi
  done > $pipe &

  wget_info=`ps ax |grep "wget.*$1" |awk '{print $1"|"$2}'`
  wget_pid=`echo $wget_info|cut -d'|' -f1 `

  zenity --progress --auto-close --text="Connecting to $1\n\n\n" --width="350" --title="Downloading"< $pipe
  if [ "`ps -A |grep "$wget_pid"`" ];then
    kill $wget_pid
  fi
  rm -f $pipe
}

if [ $1 ];then
  DOWNLOAD "$1"
else
  dllink=$(zenity --entry --text "Your download link :" --width="350" --entry-text "" --title="Download url")
  if [ $dllink ];then
    DOWNLOAD "$dllink"
  fi
fi

Via | Ubuntu Forums


Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.