模拟微调器以了解Bash的进度 [英] Simulating a spinner for progress in Bash

查看:47
本文介绍了模拟微调器以了解Bash的进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在学习如何使脚本更加冗长.下面的代码显示了一个微调器.但是,我很难修改此微调器以使其具有诸如下载"之类的词.我希望单词和微调框都出现在彼此旁边.我不是在问如何实现微调器以取得进步,而是要如何与单词连接.如何实现这个目标?

I am currently learning how to make scripts a bit more verbose. The below code shows a spinner. However, I am having difficulties modifying this spinner to have the words such as 'Downloading'. I want both the words and spinner to appear beside each other. I am not asking how to implement spinner for progress but how to concatenate with words. How could achieve this goal?

sp='/-\|'
sc=0
spin() {
   printf "\b${sp:sc++:1}"
   ((sc==${#sp})) && sc=0
}
endspin() {
   printf "\r%s\n" "$@"
}

until work_done; do
   spin
   some_work ...
done
endspin

推荐答案

虽然我钦佩Ed和Jakuje的DIY精神,但我也喜欢重用其他人的代码.如果您想回收再利用而不是重新创建,请考虑 Louis Marascio的微调框.我将他的 spinner()函数放入我的shell库中,并且易于使用:

While I admire the DIY spirit of Ed and Jakuje I also like to reuse other folks code. If you'd rather recycle than recreate consider Louis Marascio's spinner. I put his spinner() function into my shell library and it is easy to use:

#!/bin/bash

. lib.sh

run_10s &

echo -n wait
spinner $!

echo -e "\rdone"

显示

$ ./test_lib
wait [/]

旋转器旋转

10秒钟,然后清除包含 wait \ r 的那一行,而剩下的只是

for 10 seconds with the spinner spinning and then it clears that line left containing wait with the \r and you are left with just

$ ./test_lib
done
$

在屏幕上.

这篇关于模拟微调器以了解Bash的进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆