且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

是否可以使用bash脚本在终端中打印旋转光标?

更新时间:2023-01-29 19:51:23

旋转器很好,但是如果您真的想要一个可控制的进度表,该进度表知道您要处理的IO,请查看 pv .

Spinners are nice, but if you really want a controllable progress meter that is aware of the IO you're dealing with, take a look at pv.

这是一个快速而且肮脏的微调器.( sleep 的许多非标准实现会让您睡一秒钟.)

Here's a quick-and-dirty spinner. (Many nonstandard implementations of sleep will let you sleep for fractions of a second.)

spin() {
   local -a marks=( '/' '-' '\' '|' )
   while [[ 1 ]]; do
     printf '%s\r' "${marks[i++ % ${#marks[@]}]}"
     sleep 1
   done
 }

POSIX Sh

spin() {
  i=0
  marks='/ - \ |'
  while true; do
    if [ $# -lt 4 ]; then
      set -- "$@" $marks
    fi
    shift $(( (i+1) % $# ))
    printf '%s\r' "$1"
    sleep 1
  done
}