3

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

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

有没有办法让wget命令以custom_script.sh与进度条相同的方式格式化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%
4

2 回答 2

1

感谢Inian提供的链接(https://github.com/roddhjav/progressbar ),与hanshenrik类似的方法以及大量的谷歌搜索和反复试验,我想出了以下解决方案:

custom_script.sh

#!/bin/bash

source progressbar.sh || exit 1

reformat_and_output_line() {
    COLS=()
    for val in $1 ; 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:]]\)/\1 \2/')
        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:]]\)/\1 \2/')
        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 $1 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 块,但随后终端会发疯,并且无法实际读取任何值,因为它们更新得太快了。

于 2018-12-18T23:57:16.163 回答
1

我认为这听起来很奇怪,但我想你可以用正则表达式解析出 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 依赖),但我不知道如何。

于 2018-12-18T20:17:28.323 回答