MarcosBL
3/14/2014 - 8:07 PM

Bash progress bar

Bash progress bar

show_prog_bar() {
  local c="$1" # Character to use to draw progress bar
  local v=$2 # Percentage 0<= value <=100
  local t=$3 # Text before
  local pbl=50 # Progress bar length in characters
  local r=`expr 100 / $pbl` # Ratio between percentage value and progress bar length
  local p="$v%" # Percentage value to display in the middle of progress bar
  local l=${#p} # Length of string to display
  local pbs="" # Progress bar string
  local k=`expr \( $pbl - $l \) / 2` # Position where to substitute in progress bar string
  local n1=`expr $v / $r`
  local n2=`expr $pbl - $n1`
  for (( i=0; i<$pbl; i++ )); do
    pbs="${pbs}${c}"
  done
  pbs=`echo "$pbs" | sed 's/^\(.\{'"${k}"'\}\)\(.\{'"${l}"'\}\)\(.*$\)/\1'"${p}"'\3/'`
  printf "\r\e[0m${t} \e[1;42m%s\e[1;41m%s\e[0m%s" "${pbs:0:$n1}" "${pbs:$n1:$n2}" "${c}"
}

show_prog_bar " " 65 "Progreso de conversión: "