How to resize the progress bar to fit the available space?

I am looking to get an effect when the length of my progress bar changes to fit my PuTTY window. This effect is performed using the wget progress bar.

Here is my program that I use in bash scripts to create a 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 )) 
then 
    echo >&2 "current value must be smaller max. value" 
    exit 1 
fi
percent=`awk 'BEGIN{printf("%5.2f", '$current' / '$max' * 100)}'`

chars=($current*$maxwidth)/$max
echo -ne " ["

while (( $i < $maxwidth )) 
do 
    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 )); then
    echo -ne "\r"
    echo
fi

Here is an example of how I use it, this example finds all Tor Onion Exit proxies and denies IP under a custom chain:

#!/bin/bash


IPTABLES_TARGET="DROP"
IPTABLES_CHAINNAME="TOR"

WORKING_DIR="/tmp/"

# get 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 ; then            #If chain doesn't exist
    iptables -N "$IPTABLES_CHAINNAME" >/dev/null 2>&1               #Create it
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*$')

for IP in $CMD; do
    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 that the first example that people might not want to use is a simpler concept:

#!/bin/bash

for i in {1..100}; do
    _progress_bar $i 100
done
+3
source share
3 answers

I made a few changes to your script:

  • . , , . script.
  • while ( for ((i=0; $i < $maxwidth; i++)) ) .
  • , , , .
  • , .
  • echo -en printf.
  • .
  • AWK, "100.00%" .
  • AWK "": .

:

_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 ))
    then
        echo >&2 "current value must be smaller than 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 into the var named as the arg to -v
    printf -v complete '%0*.*d' '' "$chars" ''
    printf -v remain '%0*.*d' '' "$((maxwidth - chars))" ''

    # replace the zeros with the desired char
    complete=${complete//0/"$filled_char"}
    remain=${remain//0/"$blank_char"}

    printf ' [%s%s] %s%%\r' "$complete" "$remain" "$percent"
}

? , . BashFAQ/091. tput bash -i $COLUMNS. , bash -i, ,

+1

Google :

tput cols , , Sdaz COLUMNS var.

: maxwidth=$(tput cols), - , tput

+1

Bash LINES COLUMNS envvars . , SSH telnet WINCH , .

bash script COLUMNS, , 100 / progbarlen (progbarlen COLUMNS), , , . , SIGWINCH ( trap) COLUMNS envvar .

( script, , bash / .)

0

Source: https://habr.com/ru/post/1793102/


All Articles