vieyahn2017 / shellv

shell command test and study
4 stars 1 forks source link

8.29 shell进度条 #55

Closed vieyahn2017 closed 5 years ago

vieyahn2017 commented 5 years ago

shell进度条 https://blog.csdn.net/xs_520/article/details/73917884

vieyahn2017 commented 5 years ago
i=0
bar=''
index=0
arr=( "|" "/" "-" "\\" )
while true; do
    let index=index%4
    printf "[%-100s][%d%%][\e[43;46;1m%c\e[0m]\r" "$bar" "$i" "${arr[$index]}"
    let i++
    let index++
    usleep 30000
    bar+='#'
    if [ "$i" -gt 100 ]; then
        break
    fi
done
printf "\n"
vieyahn2017 commented 5 years ago

这代码怎么把业务结合起来? 所以,也不是可以拿来用的代码

vieyahn2017 commented 5 years ago

一个显示复制进度的shell(进度条按复制比例显示) 2010年12月31日 19:09:34 weixin_34368949 阅读数 21 原文链接:http://blog.51cto.com/newyorks/470063

  4 
  5   declare -i PERCENT=0
  6 
  7   dir=`dialog --stdout --title "Coping..." --inputbox "which dir do you want to do?" 7 40`
  8   choice=`dialog --stdout --title "Coping..." --inputbox "do you want go on??(y or n)" 7 40`
  9 
 10   if [ $choice = 'y' ]; then
 11   LINE=`ls -l $dir | wc -l`
 12   (
 13   for I in $dir/*;do
 14     if [ $PERCENT -le 100 ]; then
 15       cp -r $I /tmp/test 2> /dev/null
 16 
 17       echo "XXX"
 18       echo "Copy the file $I ..."
 19       echo "XXX"
 20       echo $PERCENT
 21     fi
 22     LINE=$[ $LINE-1 ]
 23     let PERCENT=$[ $[ 1`00/$LINE ]*2 ]
 24     sleep 0.1
 25   done ) | dialog --title "Coping..." --gauge "Starting to copy files..." 6 50 0
 26   
 27   else
 28     exit 1
 29   fi
 30 

这个脚本有个小bug,在复制文件数目小于100的文件夹时可以正常显示,超过100时就不正常显示。另外,不知道如何才会出现小数,请指点。 转载于:https://blog.51cto.com/newyorks/470063

vieyahn2017 commented 5 years ago
#!/bin/bash
ProgressBar()
{
  local current=$1; local total=$2
  local now=$((current*100/total))
  local last=$(((current-1)*100/total))
  [[ $((last % 2)) -eq 1 ]]&&let last++
  local str=$(for i in `seq 1 $((last/2))`; do printf '#'; done)
  for ((i=$last;$i<=$now;i+=2));do printf "\r[%-50s]%d%%" "$str"  $i;sleep 0.02;str+='#';done
}
for  n in `seq 1 100`
  do
  ProgressBar $n 100
  done
echo

https://www.jianshu.com/p/7ba06b47e0b6

vieyahn2017 commented 5 years ago

https://github.com/diveyez/fw.sh/blob/e67725b0d80df7678baca3f6c0aab52bf9b13e3a/bl.sh

参考这个,引入使用

vieyahn2017 commented 5 years ago
#!/bin/bash
#############################################
          _start=1
          _end=100
          _lastsize=0
          _currentm=""
          _cursize=0
          _lastm=""
          _lastsize=0
          _fillspaces=0
          function ProgressBar {
              let _progress=(${1}*100/${_end}*100/100)
              let _done=(${_progress}*4/10)
              let _left=40-$_done
              _fill=$(printf "%${_done}s")
              _empty=$(printf "%${_left}s")
              _lastm=${_currentm}
              _currentm=$2
              _lastsize=${#_lastm}
              _cursize=${#_currentm}
              let _sizediff=${_cursize}-${_lastsize}
              _fillspaces=$(printf "%${_sizediff}s")
              printf "\rProgress : [${_fill// /#}${_empty// /-}] ${_progress}%% : ${_currentm}${_fillspaces// / }"
          }
#############################################
vieyahn2017 commented 5 years ago
#!/bin/bash

### 进度条打印方法 progress_bar
function progress_bar {
    let _progress=$(($1*100/${_end}*100/100))
    let _done=$((${_progress}*5/10))
    let _left=50-$_done
    _fill=$(printf "%${_done}s")
    _empty=$(printf "%${_left}s")
    _lastm=${_currentm}
    _currentm=$2
    _lastsize=${#_lastm}
    _cursize=${#_currentm}
    let _sizediff=${_cursize}-${_lastsize}
    _fillspaces=$(printf "%${_sizediff}s")
    printf "\rPrecheck : [${_fill// /#}${_empty// /-}] ${_progress}%% : ${_currentm}${_fillspaces// / }"
}

### 进度条打印方法 progress_bar
### 在每个过程方法中,通过两个全局变量PROGRESS_FUNC_BEGIN和PROGRESS_FUNC_END
### 控制进度条显示的progress数字进度,把progress作为$1传到progress_bar
### progress_bar的$2为显示的提示信息

funcA () {
    local check_server_ip=$1
    local service_ip
    local service_ip_num_begin=`echo $check_server_ip | grep -o ','|wc -l`
    local service_ip_num=$(($service_ip_num_begin+1))

    for((i=0;i<$service_ip_num;i++)); do
        count=$(($i+1))
        service_ip=`echo $check_server_ip | awk -F ',' '{print $'$count'}'`
        progress=$(($PROGRESS_FUNC_BEGIN+i*$(($PROGRESS_FUNC_END - $PROGRESS_FUNC_BEGIN))/$service_ip_num))
        progress_bar ${progress} "check port at node ${service_ip}"
        log "check node ${service_ip}:"

        // business code ...
    done

}

progress=${_start}
progress_bar ${progress} "starting"

PROGRESS_FUNC_BEGIN=1
PROGRESS_FUNC_END=20
#func1

PROGRESS_FUNC_BEGIN=21
PROGRESS_FUNC_END=40
funcA

# ...

progress_bar 100 "finished"