wget 或 curl:格式化进度条

wget or curl: format progress bar

我正在编写这样的更新脚本:

update.sh

#!/bin/bash

sudo echo

printf "### PACMAN\n"   # I am running Arch Linux
sudo pacman -Syu

printf "\n### YAY\n"
yay -Syua

printf "\n### CUSTOM\n"
custom_script.sh # code below

custom_script.sh

#!/bin/bash

# [...]

wget --quiet --show-progress <some link>

# [...]

输出看起来像这样:

### PACMAN
<...>
:: Retrieving packages...
 linux-lts-4.14.88-1-x86_64                     60,8 MiB   725K/s 01:26 [########################################] 100%
<...>

### YAY
:: Searching AUR for updates...
 there is nothing to do

### CUSTOM
:: Downloading custom updates...
somefile.txt                  100%[================================================>]  20,92M  3,83MB/s    in 5,6s

有没有办法让custom_script.sh中的wget命令像pacman一样格式化进度条?我也愿意使用 curl 或其他一些下载工具。

期望的输出:

### PACMAN
<...>
:: Retrieving packages...
 linux-lts-4.14.88-1-x86_64                     60,8 MiB   725K/s 01:26 [########################################] 100%
<...>

### YAY
:: Searching AUR for updates...
 there is nothing to do

### CUSTOM
:: Downloading custom updates...
 somefile.txt                                   20,9 MiB   3,8M/s 00:05 [########################################] 100%

我觉得这听起来很奇怪,但我想你可以用正则表达式解析出 wget 的响应,然后在将信息打印到控制台之前根据需要重新格式化信息......这是一个证明 - php-cli:

中的概念
#!/usr/bin/env php
<?php
$args = $argv;
unset ( $args [0] );
$args = implode ( " ", array_map ( 'escapeshellarg', $args ) );
$wget_cmd = "wget --quiet --show-progress --progress=bar:force {$args} 2>&1";
$wget = popen ( $wget_cmd, "rb" );
$format = <<<'FORMAT'
%filename% %separator_filename_percent% %total_downloaded%    %speed% %eta% %percent_indicator% %percent%%
FORMAT;
$format = trim ( $format );
while ( ! feof ( $wget ) ) {
    $line = stream_get_line ( $wget, 4096, "\r" );
    $match = preg_match ( '/^(?<filename>.*?)(?<separator_filename_percent>[ ]{3,})(?<percent>\d+)\%(?<percent_indicator>.*?])\s+(?<total_downloaded>\S+)\s+(?<speed>\S+)\s*(?:eta\s*)?(?<eta>.*)?$/', $line, $matches );
    if (! $match) {
        echo $line;
        if (strlen ( $line ) < 4096 && ! feof ( $wget )) {
            echo "\r";
        }
    } else {
        // var_dump ( $matches );
        echo strtr ( $format, array (
                '%filename%' => $matches ['filename'],
                '%separator_filename_percent%' => $matches ['separator_filename_percent'],
                '%total_downloaded%' => $matches ['total_downloaded'],
                '%speed%' => $matches ['speed'],
                '%eta%' => $matches ['eta'],
                '%percent_indicator%' => str_replace ( "=", "#", $matches ['percent_indicator'] ),
                '%percent%' => $matches ['percent'] 
        ) ), "     ", "\r";
    }
    // sleep(3);
}
pclose ( $wget );

输出看起来像

(但可以通过编辑 $format 进行配置)

我认为整个事情都可以用 sed 重写(我假设你更喜欢 sed 依赖而不是 php-cli 依赖),但我不知道如何。

感谢 link Inian provided (https://github.com/roddhjav/progressbar), a similar approach as hanshenrik 和大量的谷歌搜索和反复试验,我想出了以下解决方案:

custom_script.sh

#!/bin/bash

source progressbar.sh || exit 1

reformat_and_output_line() {
    COLS=()
    for val in  ; do
            COLS+=("$val")
    done
    if [[ ${#COLS[@]} == 9 ]]; then
        RELATIVE_PROGRESS=${COLS[6]%?}
        ABSOLUTE_PROGRESS=$(numfmt --from=iec --to=iec-i --format "%.1fB" ${COLS[0]} |
                            sed 's/\([^[:blank:]]\)\([[:upper:]]\)/ /')
        SPEED=$(printf "%+8s" "${COLS[7]}/s")
        TIME=$(printf "%+5s" ${COLS[8]})
        progressbar "somefile.txt" "$RELATIVE_PROGRESS" 100 "$ABSOLUTE_PROGRESS" "$SPEED" "$TIME"
    elif [[ ${#COLS[@]} == 7 ]]; then
        RELATIVE_PROGRESS=${COLS[5]%?}
        ABSOLUTE_PROGRESS=$(numfmt --from=iec --to=iec-i --format "%.1fB" ${COLS[0]} |
                            sed 's/\([^[:blank:]]\)\([[:upper:]]\)/ /')
        SPEED=$(printf "%+8s" "$(echo ${COLS[6]} | cut -d= -f1 )/s")
        TIME=$(printf "%+5s" $(echo ${COLS[6]} | cut -d= -f2 ))
        progressbar "somefile.txt" "$RELATIVE_PROGRESS" 100 "$ABSOLUTE_PROGRESS" "$SPEED" "$TIME"
    fi
}

wget_like_pacman() {
    last_output_time=$(( $(date +%s%3N) - 500 ))
    wget --quiet --show-progress  2>&1 | (
        while IFS= read -r line; do
            if [[ $(date +%s%3N) > $(( $last_output_time + 500 )) ]]; then
                reformat_and_output_line "${line}"
                last_output_time=$(date +%s%3N)
            fi
        done
        printf "\r"
        reformat_and_output_line "${line}"
        echo
    )
}

# [...]

wget_like_pacman <some link>

# [...]

时间格式不完全相同,但除此之外,我认为这是非常准确的。

也可以省略检查 last_output_time 的 if 块,但终端会变得疯狂,因为更新太快,无法实际读取任何值。