Creating a spinning circle using the characters |, /, \, - in a shell script

I want to write a shell script that performs a task related to installation. I want to show some icon as a rotating circle by printing the characters |, /, \, -. When installation is complete, this circle disappears. Any help on this would be appreciated.

+6
source share
5 answers

The decision made is too complicated. You can simply:

while sleep 1; do i=$((++i%4 + 2)); printf '\b|/-\' | cut -b 1,$i | tr -d '\n'; done 

(Note that subsection adhesion is not portable, and none of them is).

+2
source

Based on Mark B's answer, here is a simple demo:

 spin () { chars="| / – \\" rotations=3 delay=0.1 for i in `seq 0 $rotations`; do for char in $chars; do echo -ne $char sleep $delay echo -ne '\b' done done } 

Paste it into the terminal and type "spin".


Update: this version works in both bash and zsh.

 spin () { char=( \| / – \\ ) charLastIndex=3 rotations=3 delay=0.1 for i in `seq 1 $rotations`; do for j in `seq 0 $charLastIndex`; do echo -n ${char[$j]} sleep $delay echo -ne '\b' done done } 

Update: The liori version works in several shells.

 spin () { rotations=3 delay=0.1 for i in `seq 0 $rotations`; do for char in '|' '/' '-' '\'; do #'# inserted to correct broken syntax highlighting echo -n $char sleep $delay printf "\b" done done } 
+4
source
 echo -e 'abc\bd' ^^---backspace char 

displays

 abd 

So basically you will output one of your animation characters, then print the backspace, then print the next char, etc.

+2
source

Here is the POSIX compatible version :

 while true; do for x in '|' '/' '-' '\'; do printf '\b$x'; sleep 1; done; done 

Please note that as a side effect, your x value will be overridden. To avoid this, include instructions in the function.

0
source

Since you will not specify which shell, a small cork for the shell for the fish , where you can do this quite elegantly using arrays:

 set -l symbols ◷ ◶ ◵ ◴ while sleep 0.5 echo -e -n "\b$symbols[1]" set -l symbols $symbols[2..-1] $symbols[1] end 

In this case, symbols is an array variable, and the contents, if it is rotated / shifted, because $symbols[2..-1] are all entries except the first.

0
source

Source: https://habr.com/ru/post/915015/


All Articles