bash - How to resize progress bar according to available space? -


i looking effect length of progress bar resizes accordingly putty window. effect accomplished wget's progress bar.

here program use in bash scripts create progress bar:

_progress_bar

#!/bin/bash  maxwidth=50     # line length (in characters) filled_char="#" blank_char="."  current=0 max=0 i=0  current=${1:-0}  max=${2:-100}   if (( $current > $max ))       echo >&2 "current value must smaller max. value"      exit 1  fi percent=`awk 'begin{printf("%5.2f", '$current' / '$max' * 100)}'`  chars=($current*$maxwidth)/$max echo -ne " ["  while (( $i < $maxwidth ))       if (( $i <= $chars ));then         echo -ne $filled_char      else         echo -ne $blank_char     fi      i=($i+1) done   echo -ne "] $percent%\r"    if (( $current == $max ));     echo -ne "\r"     echo fi 

here example of how use it, example finds tor onion proxies exit nodes , bans ip under custom chain:

#!/bin/bash   iptables_target="drop" iptables_chainname="tor"  working_dir="/tmp/"  # ip address of eth0 network interface ip_address=$(ifconfig eth0 | awk '/inet addr/ {split ($2,a,":"); print a[2]}')    if ! iptables -l "$iptables_chainname" -n >/dev/null 2>&1 ;            #if chain doesn't exist     iptables -n "$iptables_chainname" >/dev/null 2>&1               #create fi   cd $working_dir   wget -q -o - http://proxy.org/tor_blacklist.txt -u nosuchbrowser/1.0 > temp_tor_list1 sed -i 's|rewritecond %{remote_addr} \^||g' temp_tor_list1 sed -i 's|\$.*$||g' temp_tor_list1 sed -i 's|\\||g' temp_tor_list1 sed -i 's|rewrite.*$||g' temp_tor_list1  wget -q -o - "https://check.torproject.org/cgi-bin/torbulkexitlist.py?ip=$ip_address&port=80" -u nosuchbrowser/1.0 > temp_tor_list2 wget -q -o - "https://check.torproject.org/cgi-bin/torbulkexitlist.py?ip=$ip_address&port=9998" -u nosuchbrowser/1.0 >> temp_tor_list2 sed -i 's|^#.*$||g' temp_tor_list2    iptables -f "$iptables_chainname"   cmd=$(cat temp_tor_list1 temp_tor_list2 | uniq | sort) ubound=$(echo "$cmd" | grep -cve '^\s*$')  ip in $cmd;     let count=count+1     _progress_bar $count $ubound     iptables -a "$iptables_chainname" -s $ip -j $iptables_target done  iptables -a "$iptables_chainname" -j return   rm temp_tor* 

edit:

i realized first example people may not want use here more simple concept:

#!/bin/bash  in {1..100};     _progress_bar $i 100 done 

i made few changes script:

  • converted function. if want keep in separate file it's available multiple scripts, source file in each of scripts. doing eliminates overhead of repeatedly calling external script.
  • eliminated while loop (which should have been for ((i=0; $i < $maxwidth; i++)) loop anyway) drastic speed-up.
  • changed arithmetic expressions evaluate instead of setting them strings later evaluation.
  • removed dollar signs variable names appear in arithmetic contexts.
  • changed echo -en printf.
  • made few other changes
  • changed awk output "100.00%" decimal aligned smaller values.
  • changed awk command use variable passing instead of "inside-out: quoting.

here result:

_progress_bar () {     local maxwidth=50     # line length (in characters)     local filled_char="#"     local blank_char="."      local current=0 max=0 i=0     local complete remain      current=${1:-0}     max=${2:-100}      if (( current > max ))             echo >&2 "current value must smaller max. value"         return 1     fi     percent=$(awk -v "c=$current" -v "m=$max" 'begin{printf("%6.2f", c / m * 100)}')      (( chars = current * maxwidth / max))      # sprintf n zeros var named arg -v     printf -v complete '%0*.*d' '' "$chars" ''     printf -v remain '%0*.*d' '' "$((maxwidth - chars))" ''      # replace zeros desired char     complete=${complete//0/"$filled_char"}     remain=${remain//0/"$blank_char"}      printf ' [%s%s] %s%%\r' "$complete" "$remain" "$percent" } 

what question? oh, see bashfaq/091. use tput or bash -i , $columns. if use bash -i, however, aware have overhead of processing startup files


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -