firefox中有個功能很有用,就是輸入g然後後面加上要搜尋的關鍵字,就可以自動到google上搜尋。這個功能叫QuickSearch。
我希望在命令列下也有這個功能,所以自己寫了名為search的bash script。可以在命令列中直接調用文本介面的瀏覽器搜尋(例如w3m或者lynx).
使用方法如下:
在google中搜尋索引鍵:
$search g keyword [keyword]...
在google blogsearch中搜尋索引鍵:
$search b keyword [keyword]...
查辭典:
$search d keyword
如果你設定好alias(實際上search會幫你做得):
那麼上面的的三種搜尋可以簡化為
$g keyword [keyword]...
$b keyword [keyword]...
$d keyword
搜尋引擎是可以定製的,定製的過程及其簡單,和firefox的QuickSearch的設定沒有區別。
將search安裝到/usr/bin目錄:
$search install
輸出設定alias的指令碼到標準輸出:
$search mkalias
典型的安裝過程:
$su
$search install;exit
$search mkalias >> ~/.bashrc; source ~/.bashrc
以下是原始碼:#!/bin/sh
# Written by redguardtoo <http://blog.csdn.net/redguardtoo>
SEARCH_ALIAS=(g r n wi c b d)
SEARCH_URL=("http://www.google.com/search?q=%s&ie=utf-8&oe=utf-8"
"http://groups.google.com/groups?q=%s&sourceid=opera&num=%i&ie=utf-8&oe=utf-8"
"http://news.google.com/news?q=%s&sourceid=opera&num=%i&ie=utf-8&oe=utf-8"
"https://secure.wikimedia.org/wikipedia/en/wiki/Special:Search?search=%s"
"https://secure.wikimedia.org/wikipedia/zh/wiki/Special:Search?search=%s"
"http://blogsearch.google.com/blogsearch?hl=en&ie=UTF-8&q=%s&btnG=Search+Blogs"
"http://www.m-w.com/dictionary/%s"
)
BROWSER_PROG=w3m
BROWSER_OPTION="-O GBK -cookie"
PROG=`basename $0`
#if [ $(whoami) != 'root' ]; then
#echo "Must be root to run $0"
#exit 1;
#fi
if [ -z $1 ]; then
echo "search version 0.0.1"
echo "usage: $PROG alias keyword"
echo " $PROG install"
echo " $PROG uninstall"
echo " $PROG mkalias"
exit 1
fi
if [ "$1" == "install" ];then
echo "Installing..."
cp $0 /usr/bin/$PROG
exit 0
fi
if [ "$1" == "uninstall" ];then
echo "Uninstalling..."
rm /usr/bin/$PROG
exit 0
fi
if [ "$1" == "mkalias" ];then
for sa in ${SEARCH_ALIAS[@]};do
echo alias $sa="/usr/bin/$PROG $sa"
done
exit 0
fi
QUERY_SHORTCUT=$1
#echo $# #debug
if [ $# -gt 1 ];then
shift
QUERY_PARA=$*
#echo $QUERY_PARA #debug
fi
count=0
for sa in ${SEARCH_ALIAS[@]};do
if [ "$QUERY_SHORTCUT" == "$sa" ];then
QUERY_URL=${SEARCH_URL[$count]}
QUERY_URL=${QUERY_URL//"%s"/"$QUERY_PARA"}
QUERY_URL=${QUERY_URL//" "/"+"}
#echo $QUERY_URL #debug
$BROWSER_PROG $BROWSER_OPTION ${QUERY_URL/"%s"/"$QUERY_PARA"}
fi
count=$(($count + 1))
done