2011-02-18 78 views
3

我期待得到一个效果,其中我的进度条的长度相应地调整到我的PuTTY窗口。这个效果是用wget的进度条完成的。如何根据可用空间调整进度条的大小?

这是我的节目,我在我的bash脚本用来创建一个进度条:

_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 

下面是我如何使用它的一个例子,这个例子查找所有的Tor洋葱代理出口节点和一个自定义的链下禁止该IP:

#!/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* 

编辑:

我意识到,第一个例子中的人可能不希望因此这里用的是一个更简单的概念:

#!/bin/bash 

for i in {1..100}; do 
    _progress_bar $i 100 
done 
+0

您是否知道`sed`命令序列ca ñ结合? sed's ...; s ...; s ...'filename`或`sed -e's ...'-e's ...'-e's ...'filename`和`wget`可以传入它? `wget ... | sed ...> filename“(你也可以用一个管道作为第二个集合:`{wget ...; wget ...;} | sed ...> filename`)。 – 2011-02-19 02:38:21

回答

1

更防弹方式,我做你的脚本进行一些更改:

  • 它转换成一个功能。如果您想将它保存在单独的文件中,以便它可用于多个脚本,则只需在每个脚本中输入文件即可。这样做消除了重复调用外部脚本的开销。
  • 消除了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使用tputbash -i$COLUMNS。但是,如果您使用bash -i,请注意,它将具有处理启动文件的开销

0

猛砸分别出口的LINESCOLUMNS envvars中的窗口行和列数。此外,当您通过SSH或Telnet协议调整腻子窗口大小时,协议中有足够的逻辑向主动shell发送一个WINCH信号,然后将这些值重置为新的窗口维度。

在你的bash脚本,使用COLUMNS变量设置当前尺寸,并划分100/progbarlen(progbarlen基于列变量的部分),以获得多少个百分点,弥补了一个字符,并推动他们为你的进步一起移动。要动态处理大小调整,请为SIGWINCH(通过trap)添加处理程序,并重新读取COLUMNS envvar,然后使用新维度重新绘制进度栏。

(我还没有在shell脚本进行了测试,有可能需要一些额外的逻辑,但是这是bash的如何检测/手柄调整。)

+0

$ COLUMNS似乎在bash脚本中没有任何价值,但是从终端echo $ COLUMNS似乎工作。 – ParoX 2011-02-18 17:49:20

+0

啊,你是对的。上面的`tput`解决方案看起来不错,而且似乎也会在调整大小时进行更新。 – 2011-02-18 17:57:29

1

经过一番搜索谷歌我没有找到以下内容:

tput cols将返回列的数量,很像Sdaz建议的COLUMNS var。

所以我跟去: maxwidth=$(tput cols)除非别人有而无需tput

相关问题