击进度条[复制](Bash progress bar [duplicate])

2019-06-26 11:31发布

这个问题已经在这里有一个答案:

  • 如何添加一个进度条到shell脚本? 36个回答

我使用下面的脚本要经过的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

然后这个 ,但没有运气。

那你估计是得到落实成文字的进度条最简单的方法?

Answer 1:

下面是您可以享受一个奇特的进度条...

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

看起来是这样的:



Answer 2:

您可以使用pv ,但其他的方式。

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

所以你用echo X说工作时的另一部分完成的,这是由PV数,它是知道整个作业大小是由于-s选项。



Answer 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



Answer 4:

更改外环到:

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

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

或者你可以使用任何其他程序提供了基于多少文件被读取进度条。



Answer 5:

既然你在,你是一个基于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


Answer 6:

我建议你使用Xdialog,Kdialog或zenity和它的进步选项。



文章来源: Bash progress bar [duplicate]