2017-11-11 181 views
1

这是我的bash文件的一部分。我需要的输出是:微调框动画和回显命令

[ - ]版权所有KatworX©技术。由阿琼·辛格Kathait开发和调试由☆堆栈溢出社区☆

我想在显示echo命令微调动画继续旋转5秒。社区可以帮助吗?

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☆" 
+0

看一看[多种颜色上的bash微调的输出( https://stackoverflow.com/questions/46966891/multiple-colors-on-output-of-bash-spinner)。您可以忽略颜色的变化。但在while循环中调用'ps','awk'和'grep'的效率非常低。 –

+0

你能否修改此代码段@DavidC.Rankin并将其发布到评论框中? –

回答

2

继续评论。为避免在每次迭代中调用ps,awkgrep,您需要将PID作为参数传递给旋转函数。 (并且你可以传递一个字符串来显示,并且默认为你的字符串)。我会做类似的东西:

#!/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去除颜色)

+0

非常感谢@David。像你这样的人让这个社区变得更美好。我已经将我当前的项目专用于Stack Overflow Community,因为它在调试等方面有很多帮助。 –

+0

当然,很高兴提供帮助。你甚至可以添加一个'local delay =“$ {3:-0.1}”',并且如果你想进行调整,也可以选择延迟。祝你好运,你的脚本。 –

+0

你会替换'while [-d/proc/$ PROC];用'while'kill'$ {PROC} 2>/dev/null;做'以使其跨平台 – nbari