I am writing an update script like that:
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>
# [...]
The output looks something like this:
### 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
Is there a way to make the wget command in custom_script.sh format the progress bar the same way as pacman? I'm open to use curl or some other download tool as well.
Desired output:
### 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%
i think that sounds weird as hell, but i suppose you could parse out the response of wget with regex, then re-format the information however you want before printing it to the console... here is a proof-of-concept in 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 );
output looks like
(but is very configurable by editing $format)
i think the whole thing could be re-written with sed (and i assume you'd prefer a sed dependency over a php-cli dependency), but i don't know how.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With