微调器动画和回声命令 [英] Spinner Animation and echo command

查看:51
本文介绍了微调器动画和回声命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的bash文件的一部分.我需要的输出是:

This is a part of my bash file. The output I need is:

[-] KatworX©Tech的版权.由Arjun Singh Kathait开发并由☆Stack Overflow社区进行调试☆

我希望微调器动画在显示echo命令的同时继续旋转5秒钟.社区可以帮忙吗?

I want the spinner animation to continue spinning for 5 seconds while the echo command is being displayed. Can the community help???

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

推荐答案

从注释继续.为了避免在每次迭代中调用psawkgrep,您需要将PID作为自变量传递给spin函数. (您也可以传递一个字符串来显示,也可以默认使用该字符串).我会做类似的事情:

Continuing from the comment. To avoid calling ps, awk and grep on every iteration, you need to pass the PID as an argument to the spin function. (and you can pass a string to display and default to your string as well). I would do something similar to:

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

(it displays in blue -- but you can delete the first printf to remove the color)

这篇关于微调器动画和回声命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆