Bash进度条

时间:2012-07-21 13:27:04

标签: bash progress-bar

我正在使用以下脚本浏览whois中的大型域名列表并找到注册商(对服务器/ DNS迁移很有用),并且工作正常。

但是,为了方便起见,我想将进度条纳入其中。这是我的剧本,如果可以改进,请告诉我:

#!/bin/bash
for f in `cat /var/www/vhosts/domainlist`
 do
   if
   domain=$f
   [ "$domain" ] ;
   then
    whois $f | grep -i domainregistrar > /dev/null
     if
     [ $? -le 0 ] ;
     then
      echo $f >> our_registrar
     else
      echo $f >> external_registrar
     fi
   fi
 done
echo "Done, check our_registrar file."

我先试过这个:http://moblog.bradleyit.com/2010/02/simple-bash-progress-bar-function.html

然后this但没有运气。

您认为在该脚本中实现进度条的最简单方法是什么?

6 个答案:

答案 0 :(得分:15)

这是一个你可能喜欢的花哨的进度条......

#!/bin/bash
#   Slick Progress Bar
#   Created by: Ian Brown (ijbrown@hotmail.com)
#   Please share with me your modifications
# Functions
PUT(){ echo -en "\033[${1};${2}H";}  
DRAW(){ echo -en "\033%";echo -en "\033(0";}         
WRITE(){ echo -en "\033(B";}  
HIDECURSOR(){ echo -en "\033[?25l";} 
NORM(){ echo -en "\033[?12l\033[?25h";}
function showBar {
        percDone=$(echo 'scale=2;'$1/$2*100 | bc)
        halfDone=$(echo $percDone/2 | bc) #I prefer a half sized bar graph
        barLen=$(echo ${percDone%'.00'})
        halfDone=`expr $halfDone + 6`
        tput bold
        PUT 7 28; printf "%4.4s  " $barLen%     #Print the percentage
        PUT 5 $halfDone;  echo -e "\033[7m \033[0m" #Draw the bar
        tput sgr0
        }
# Start Script
clear
HIDECURSOR
echo -e ""                                           
echo -e ""                                          
DRAW    #magic starts here - must use caps in draw mode                                              
echo -e "          PLEASE WAIT WHILE SCRIPT IS IN PROGRESS"
echo -e "    lqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqk"  
echo -e "    x                                                   x" 
echo -e "    mqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj"
WRITE             
#
# Insert your script here
for (( i=0; i<=50; i++ ))  
do
    showBar $i 50  #Call bar drawing function "showBar"
    sleep .2
done
# End of your script
# Clean up at end of script
PUT 10 12                                           
echo -e ""                                        
NORM

看起来像这样:

looks like this

答案 1 :(得分:7)

您可以使用pv,但另一种方式。

 for ... # outer loop
 do
   ...
   echo -n X
 done | pv -s $(wc -l 'your_file_list') - >/dev/null 

所以你使用echo X说明其他部分的工作何时完成并计算在内 通过pv,它知道整个作业大小是由-s选项引起的。

答案 2 :(得分:3)

您可以使用以下内容:

progress(){
    # example usage:
    # progress 30G 9G 30
    # 30G [================>.................................] 30% (9G)

    # params:
    # $1 = total value (e.g.: source size)
    # $2 = current value (e.g.: destination size)
    # $3 = percent completed
    [[ -z $1 || -z $2 || -z $3 ]] && exit  # on empty param...

    percent=$3
    completed=$(( $percent / 2 ))
    remaining=$(( 50 - $completed ))

    echo -ne "\r$1 ["
    printf "%0.s=" `seq $completed`
    echo -n ">"
    [[ $remaining != 0 ]] && printf "%0.s." `seq $remaining`
    echo -n "] $percent% ($2)  "
}

来自https://gist.github.com/ivanalejandro0/9159989

您可以在https://github.com/ivanalejandro0/misc/blob/master/shell-scripts/copy-progress.sh

中查看用法示例

答案 3 :(得分:1)

将外循环更改为:

pv /var/www/vhosts/domainlist | while read f
do
    ...
done

http://linux.die.net/man/1/pv

或者您可以根据文件的读取次数使用提供进度条的任何其他程序。

答案 4 :(得分:1)

鉴于您在评论中提到您使用的是基于debian的系统,您可以使用whiptail。当您安装需要配置的deb软件包时,会绘制基于文本的窗口来询问您的内容;这是whiptail

这样的东西
#!/usr/bin/env bash

# mapfile requires bash 4
mapfile -t domains < /var/www/vhosts/domainlist

# for older bash versions, read can be used in this case.
#IFS=$'\n' read -rd '' -a domains < /var/www/vhosts/domainlist

n=${#domains[@]}

for ((i=0; i < n; ++i)); do
    printf 'XXX\n\n%s\nXXX\n' "Checking ${domains[i]}"
    if whois "${domains[i]}" | grep -Fiq domainregistrar; then
        printf '%s\n' "${domains[i]}" >&3
    else
        printf '%s\n' "${domains[i]}" >&4
    fi
    printf '%d\n' $((100*i/n))
done 3>our_registrar 4>external_registrar | whiptail --gauge "" 6 50 0

答案 5 :(得分:0)

我建议您使用Xdialog,Kdialog或zenity,它是进度选项。