我正在编写这样的更新脚本:
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%
答案 0 :(得分:1)
我认为这听起来很奇怪,但是我想您可以使用regex解析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 );
输出类似于
我认为整个事情都可以用sed重写(并且我认为您更喜欢sed依赖而不是php-cli依赖),但是我不知道如何。
答案 1 :(得分: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>
# [...]
时间格式不完全相同,但除此之外,我认为这是非常准确的。
还可以省略if函数块,以检查last_output_time,但是终端发疯了,由于更新太快,因此无法实际读取任何值。