2017-11-11 179 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