常用 watch 指令
偏偏 watch 不能顯示中文
剛剛終於決定狠心寫一個 script
想想對 watch 的不滿 , 還有 ..
1. clear 完 , 資訊總在螢幕最上方 => 所以改最下方且不 clear
2. 過多資訊總是 head 前幾行 -> 塞指令為什麼不加 tail => 不管 ... 加個 tail
3. 因為要捕捉 ctrl-c , 第?次再看很難的 trap 指令 , 這次只瞄幾秒 , 突然覺得好簡單
這決心是聯想到上此討論 head 與 tail 中斷是誰決定
所以 script 中那行
while true ;do echo ;done | head -$maxn
算是起點吧 ...
#!/bin/bash
# 中文
hhelp(){
echo $(basename $0)'
-n 幾秒循環
-head 超過螢幕用 head 取樣
-tail 超過螢幕用 tail 取樣
-h|-help|--help
'
exit
}
HT=/usr/bin/head
sec=1
while [ "$1" != "" ] ; do
case $1 in
-n)
sec=$2
shift 2
;;
-head)
HT=/usr/bin/head
shift
;;
-tail)
HT=/usr/bin/tail
shift
;;
-h|-help|--help)
hhelp
;;
*)
cmd=$cmd' '$1
shift
;;
esac
done
trap 'tput cup $(tput lines) 0;echo exit;exit' 1 2 3 15
echo '-------------------------------------------------------------'
echo '執行 '$cmd' / 每 '$sec' 秒'
echo '-------------------------------------------------------------'
maxn=0
while true;do
monitor=$(tput lines)
l=$(($monitor-3))
ans=$(eval "$cmd" | $HT -$l)
n=$(echo "$ans" | wc -l)
if [ $n -ge $maxn ] ; then maxn=$n ; fi
mysc=$(($monitor-$maxn-1))
while true ;do echo ;done | head -$maxn
tput cup $mysc 0
for((i=$mysc;i<$monitor;i++));do
tput cup $i 0
tput el
done
tput cup $mysc 0
echo "$ans"
tput cup $monitor $(tput cols)
sleep $sec
tput cup $mysc 0
done