Bash 微调器动画和回音命令
这是我的bash文件的一部分。我需要的输出是: [-]由Arjun Singh Kathait开发并由☆堆栈溢出社区☆强> 我希望微调器动画在显示echo命令时继续旋转5秒。社区能帮忙吗Bash 微调器动画和回音命令,bash,shell,unix,terminal,gnome-terminal,Bash,Shell,Unix,Terminal,Gnome Terminal,这是我的bash文件的一部分。我需要的输出是: [-]由Arjun Singh Kathait开发并由☆堆栈溢出社区☆ 我希望微调器动画在显示echo命令时继续旋转5秒。社区能帮忙吗 spinner() { local pid=$! local delay=0.75 local spinstr='|/-\' while [ "$(ps a | awk '{print $1}' | grep $pid)" ]; do
spinner()
{
local pid=$!
local delay=0.75
local spinstr='|/-\'
while [ "$(ps a | awk '{print $1}' | grep $pid)" ]; do
local temp=${spinstr#?}
printf " [%c] " "$spinstr"
local spinstr=$temp${spinstr%"$temp"}
sleep $delay
printf "\b\b\b\b\b\b"
done
}
sleep 5 & spinner | echo -e "\nCopyright of KatworX© Tech. Developed by Arjun Singh Kathait and Debugged by the ☆Stack Overflow Community☆"
继续评论。为了避免在每次迭代中调用
ps
、awk
和grep
,需要将PID作为参数传递给spin函数。(您可以传递要显示的字符串,也可以将默认值传递给您的字符串)。我会做类似的事情:
#!/bin/bash
## spinner takes the pid of the process as the first argument and
# string to display as second argument (default provided) and spins
# until the process completes.
spinner() {
local PROC="$1"
local str="${2:-'Copyright of KatworX© Tech. Developed by Arjun Singh Kathait and Debugged by the ☆Stack Overflow Community☆'}"
local delay="0.1"
tput civis # hide cursor
printf "\033[1;34m"
while [ -d /proc/$PROC ]; do
printf '\033[s\033[u[ / ] %s\033[u' "$str"; sleep "$delay"
printf '\033[s\033[u[ — ] %s\033[u' "$str"; sleep "$delay"
printf '\033[s\033[u[ \ ] %s\033[u' "$str"; sleep "$delay"
printf '\033[s\033[u[ | ] %s\033[u' "$str"; sleep "$delay"
done
printf '\033[s\033[u%*s\033[u\033[0m' $((${#str}+6)) " " # return to normal
tput cnorm # restore cursor
return 0
}
## simple example with sleep
sleep 5 &
spinner $!
(它以蓝色显示——但您可以删除第一个
printf
以删除颜色)查看。你可以忽略颜色的变化。但是在一个while循环中调用ps
、awk
和grep
效率非常低。你能修改这个代码段@DavidC.Rankin并将其发布在评论框中吗?非常感谢@David。是像你们这样的人让这个社区变得更好。我将当前的项目奉献给Stack Overflow社区,因为它在调试等方面帮助了很多。当然,很高兴能提供帮助。您甚至可以添加一个本地延迟=“${3:-0.1}”
并将延迟作为一个选项传递,如果您想使其也可调整的话。祝您的脚本编写工作好运。您将在[-d/proc/$proc]时替换;在kill-0${PROC}2>/dev/null时使用执行
;如果它不是Linux平台,那么请密切注意mankill
语法。kill-0${PROC}2>/dev/null
应该可以工作——只要-0
信号不发送任何东西,只会像在Linux上一样报告错误。哪里有kill
但没有procfs?@DavidC.Rankin通常在“unix”中,FreeBSD有一个很好的pwait
,但在macOs中,kill-0$pid
正在发挥作用