unix 微调器动画和回显命令

vltsax25  于 2022-11-23  发布在  Unix
关注(0)|答案(1)|浏览(177)

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

[ - ] KatworX© Tech版权所有。由Arjun Singh Kathait开发,由☆Stack Overflow社区调试☆

我想在显示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☆"
enyaitl3

enyaitl31#

从注解继续。为了避免在每次迭代中调用psawkgrep,你需要将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 $!

(it显示为蓝色--但您可以删除第一个printf来移除颜色)

相关问题