linux落字效果代码,Shell中实现飞行文字效果
大家肯定見過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 1
txt=($(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è)置
clear
echo -ne "\033[?25l" #隱藏光標(biāo)
spaces() {
local i j n
i=0; n=0
for (( j=0; j <= ${lines}; j++ ));do
[ $i -lt ${#txt[$j]} ] && (( i = ${#txt[$j]} )) && (( n = $j ))
done
echo ${txt[$n]}|sed 's/./ /g'
}
space_line=$(spaces)
#傳遞的參數(shù)是欲顯示行的序數(shù):show 1 表示第一行高亮顯示
show() {
local i j text crt_line
j=$1
for (( i=0; i < max_show; i++ ));do
crt_line=$(( j + i - hlt_line ))
if [ $i -eq ${hlt_line} ] && [ ${crt_line} -ge 0 ] && [ ${crt_line} -le ${lines} ];then
text="\033[;32m"${txt[${crt_line}]}"\033[0m"
elif [ ${crt_line} -lt 0 ] || [ ${crt_line} -gt ${lines} ];then
text=${space_line}
else
text=${txt[${crt_line}]}
fi
echo -ne "\033[$((i+3));0H${space_line}"
echo -ne "\033[$((i+3));6H${text}"
done
}
#測試
for (( j = 0; j < lines + hlt_line +1 ; j++ ));do
show $j
sleep 1
done
恢復(fù)默認(rèn)設(shè)置方法:
echo ""
stty ${sTTY} #恢復(fù)終端設(shè)置
echo -e "\033[?25h\033[0;0H" #恢復(fù)光標(biāo)
總結(jié)
以上是生活随笔為你收集整理的linux落字效果代码,Shell中实现飞行文字效果的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux debian 自动安装,de
- 下一篇: linux链接curl库,Linux利用