gpt4 book ai didi

bash -\r 不能在 awk 的 printf 中工作?

转载 作者:行者123 更新时间:2023-11-29 09:49:24 24 4
gpt4 key购买 nike

通过谷歌搜索找到这个脚本,它有一个 cp 进度条,问题是它创建了很多行,如下所示:

0% [=>
0% [=>
0% [=>
0% [=>
0% [=>
0% [=>
0% [=>
0% [=>
0% [=>
0% [=>
0% [=>
1% [==>
1% [==>
1% [==>
1% [==>
1% [==>
1% [==>
1% [==>
1% [==>
1% [==>
1% [==>
1% [==>
1% [==>
1% [==>
1% [==>
1% [==>

这是脚本的代码:

#!/bin/bash

strace -q -ewrite cp -- `printf '%q ' $@` 2>&1 \
| awk '{
count += $NF
if (count % 10 == 0) {
percent = count / total_size * 100
printf "%3d%% [", percent
for (i=0;i<=percent;i++)
printf "="
printf ">"
for (i=percent;i<100;i++)
printf " "
printf "]\r"
}
}
END { print "" }' total_size=$(stat -c '%s' "${1}") count=0

还有一件事,====== 的行太长了。就像,当接近 90 年代时,==== 溢出到下一行。我怎样才能编辑它以便打印 75大约几行,因为我的默认终端是 80 宽。

编辑:好的,我意识到,如果文件中有空格,这将不起作用。 [它什么都不做,在我输入命令后立即退出]。有什么想法吗?

谢谢。 :)

最佳答案

去掉`printf "]\r"之前的printf "":

#!/bin/bash

strace -q -ewrite cp -- `printf '%q ' $@` 2>&1 \
| awk '{
count += $NF
if (count % 10 == 0) {
percent = count / total_size * 100
printf "%3d%% [", percent
for (i=0;i<=percent;i++)
printf "="
printf ">"
for (i=percent;i<100;i++)
printf "]\r"
}
}
END { print "" }' total_size=$(stat -c '%s' "${1}") count=0

此外,您的终端窗口需要足够宽以处理所有字符(110 个字符或更多)。

关于bash -\r 不能在 awk 的 printf 中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6725748/

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