大家肯定見過WEB頁面上飛行字的效果,但是在Linux系統(tǒng)中,特別是bash命令行模式下,如何實(shí)現(xiàn)echo出來的字符串以飛行的特效展現(xiàn)出來呢?
這個(gè)特效不見得實(shí)用,但偶爾拿來自娛自樂或炫耀一番倒是個(gè)不錯(cuò)的方法,呵呵,下面以一段Shell Script代碼來給大家講解如何實(shí)現(xiàn)……
#!/bin/bash[ -z $1 ] && echo "Input is invalid! " && exit 1txt=($(cat $1|sed 's/ /+/_/g'))lines=${#txt[@]}max_show=10 #最多顯示行數(shù)(超過總行數(shù)則設(shè)為總行數(shù))[ ${max_show} -gt ${lines} ] && max_show=${lines}hlt_line=3 #高亮顯示位置(不超過總行1/2,設(shè)為3實(shí)際是第4行)[ ${hlt_line} -gt $(( lines / 2 )) ] && hlt_line=$(( lines / 2 ))sTTY=$(stty -g) #保存終端設(shè)置clearecho -ne "/033[?25l" #隱藏光標(biāo)spaces() {local i j ni=0; n=0for (( j=0; j <= ${lines}; j++ ));do[ $i -lt ${#txt[$j]} ] && (( i = ${#txt[$j]} )) && (( n = $j ))doneecho ${txt[$n]}|sed 's/./ /g'}space_line=$(spaces)#傳遞的參數(shù)是欲顯示行的序數(shù):show 1 表示第一行高亮顯示show() {local i j text crt_linej=$1for (( i=0; i < max_show; i++ ));docrt_line=$(( j + i - hlt_line ))if [ $i -eq ${hlt_line} ] && [ ${crt_line} -ge 0 ] && [ ${crt_line} -le ${lines} ];thentext="/033[;32m"${txt[${crt_line}]}"/033[0m"elif [ ${crt_line} -lt 0 ] || [ ${crt_line} -gt ${lines} ];thentext=${space_line}elsetext=${txt[${crt_line}]}fiecho -ne "/033[$((i+3));0H${space_line}"echo -ne "/033[$((i+3));6H${text}"done}#測(cè)試for (( j = 0; j < lines + hlt_line +1 ; j++ ));doshow $jsleep 1done
恢復(fù)默認(rèn)設(shè)置方法:
echo ""stty ${sTTY} #恢復(fù)終端設(shè)置echo -e "/033[?25h/033[0;0H" #恢復(fù)光標(biāo)
新聞熱點(diǎn)
疑難解答
圖片精選