I want to write a shell script which does installation related task. I want to show some icon like rotating circle by printing characters |, /, \, -. Once the installation completes this circle will disappear. Any help on this would be appreciated.
Building on Marc B's answer, here's 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 in your 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: liori's version works in multiple 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
}
The accepted solution is overly complicated. You can just do:
while sleep 1; do
i=$((++i%4 + 2));
printf '\b|/-\' | cut -b 1,$i | tr -d '\n';
done
(Note that subsecond sleeping is not portable, and neither is seq.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With