需要一个用于旋转光标的 shell 脚本
我需要一个用于旋转光标的 shell 脚本,我可以将其与“复制”功能一起使用。我尝试了下面的程序并且它有效,但我遇到的唯一问题是微调器显示在文本下方。
#!/bin/bash
spinner=('\' '/' '-' '\')
copy(){
echo "copying files..."
spin &
pid=$!
for i in `seq 1 10`
do
sleep 1
done
kill $pid
echo ""
}
spin(){
while [ 1 ]
do
for i in "${spinner[@]}"
do
echo -ne "\r$i"
sleep 0.2
done
done
}
copy
预期输出:正在复制文件...\
I need a shell script for spinning cursor which I can use with "copying" function. I tried below program and it works but the only problem I have here is that the spinner is showing below the text.
#!/bin/bash
spinner=('\' '/' '-' '\')
copy(){
echo "copying files..."
spin &
pid=$!
for i in `seq 1 10`
do
sleep 1
done
kill $pid
echo ""
}
spin(){
while [ 1 ]
do
for i in "${spinner[@]}"
do
echo -ne "\r$i"
sleep 0.2
done
done
}
copy
Expected Output: Copying files...\
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据您使用此功能的方式,您可能希望先隐藏光标,然后再显示它。而不是为多次
copy
或spinner
调用打开和关闭它。例如:当
tput civis
运行时,光标隐藏,当脚本退出时(正常情况下,或由于中断等),光标取消隐藏(tput cvvis
)。如果您这样做,请从函数中删除相应的 tput 命令(但保留其他陷阱命令)。隐藏光标的原因是它可能会扰乱旋转动画。
Depending on how you use this, you probably want to hide the cursor earlier, and show it later. Instead of turning it on and off for multiple
copy
orspinner
invocations. Eg:The cursor is hidden when
tput civis
runs, and un-hidden (tput cvvis
) when the script exits (normally, or due to interrupt etc). If you do it like this, remove the correspondingtput
commands from the function (but keep the other trap commands).The reason for hiding the cursor is that it can mess with the spinner animation.
如果您不需要换行符,请打印文本 Copying files... 且不带尾随换行符:
Print the text Copying files... without trailing newline, if you don't want one: