gpt4 book ai didi

bash - wget 或 curl : format progress bar

转载 作者:行者123 更新时间:2023-12-04 15:51:17 32 4
gpt4 key购买 nike

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

更新.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 );

输出看起来像

enter image description here(但是可以通过编辑 $format 进行配置)

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

关于bash - wget 或 curl : format progress bar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53791437/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com