首页 > 学技术 > 技术网文 > Linux Shell > 正文

[原创] 模拟BSD风格的history的一个脚本


来源 chinaunix.net 酷勤网整理

论坛上有朋友问过的问题,见原贴: http://bbs.chinaunix.net/viewthread.php?tid=669747&extra=page%3D2
我在我的redhat上写了一个,比较粗糙点,望大家给个意见 :-)
引用:
#!/bin/bash
#名称:hist
#BSD风格的history命令
#为避免和真正的history命令冲突,可以在/etc/inputrc
#中加入:Control-a:"/path/hist \C-m" //按ctrl+A启动此history命令
#当然,最好别和其他的ctrl组合冲突
#按键盘说明:^[[A和^[[B上下翻页键,[color=blue]^[[/color]由ctrl+v+[得到
#redhat+bash测试通过 :=)
#得到[color=red][u]r2007[/u][/color]兄指点,特此感谢! ^_^
#BEGIN
#
echo -ne '\e[6n';read -sdR pos
pos=${pos#*[}
line=${pos%%;*}
col=${pos##*;}
hint='History command list>>'
file="$HOME/.bash_history"
cat $HOME/.bash_history
doo () {
while true;do
        echo -ne "\\033[31m\\033[$line;${col}H$hint\\033[0m"
        read -s -n3
        case $REPLY in
        [color=blue]^[[/color][A)   echo -en "\\033[A";((i--));echo -ne "\\033[$line;${#hint}H$i=>$(sed -n "$i"p $1)\\033[K"
                ;;
        [color=blue]^[[/color][B)   echo -ne "\\033[B";((i++));echo -ne "\\033[$line;${#hint}H$i=>$(sed -n "$i"p $1)\\033[K"
                ;;
        "")     echo;eval $(sed -n "$i"p $1) 2>/dev/null;break
                ;;
        esac
done
echo -e "\\033[0m"
}
read -s -n1 char
if [[ -n $char ]];then
        sed -n "/^$char/p" $file|tee tmp$$
        i=$(sed -n '$=' tmp$$)
        doo tmp$$
        rm tmp$$ 2>/dev/null
else
        i=$(sed -n '$=' $file)
        doo $file
fi
#END





 大蚂蚁 回复于:2005-12-22 07:50:04

火哥,对于俺这种新手,你最好注释全点 哈哈


 liinaazzw 回复于:2005-12-25 08:04:06

有截图吗?谢谢。




原文链接:http://bbs.chinaunix.net/viewthread.php?tid=674015
转载请注明作者名及原文出处



收藏本页到: