命令行快捷搜索internet的方法
命令行快捷搜索internet的方法
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
#Writtenbyredguardtoo<http://blog.csdn.net/redguardtoo>
SEARCH_ALIAS=(grnwicbd)
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="-OGBK-cookie"
PROG=`basename$0`
#if[$(whoami)!='root'];then
#echo"Mustberoottorun$0"
#exit1;
#fi
if[-z$1];then
echo"searchversion0.0.1"
echo"usage:$PROGaliaskeyword"
echo"$PROGinstall"
echo"$PROGuninstall"
echo"$PROGmkalias"
exit1
fi
if["$1"=="install"];then
echo"Installing..."
cp$0/usr/bin/$PROG
exit0
fi
if["$1"=="uninstall"];then
echo"Uninstalling..."
rm/usr/bin/$PROG
exit0
fi
if["$1"=="mkalias"];then
forsain${SEARCH_ALIAS[@]};do
echoalias$sa="/usr/bin/$PROG$sa"
done
exit0
fi
QUERY_SHORTCUT=$1
#echo$##debug
if[$#-gt1];then
shift
QUERY_PARA=$*
#echo$QUERY_PARA#debug
fi
count=0
forsain${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
#Writtenbyredguardtoo<http://blog.csdn.net/redguardtoo>
SEARCH_ALIAS=(grnwicbd)
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="-OGBK-cookie"
PROG=`basename$0`
#if[$(whoami)!='root'];then
#echo"Mustberoottorun$0"
#exit1;
#fi
if[-z$1];then
echo"searchversion0.0.1"
echo"usage:$PROGaliaskeyword"
echo"$PROGinstall"
echo"$PROGuninstall"
echo"$PROGmkalias"
exit1
fi
if["$1"=="install"];then
echo"Installing..."
cp$0/usr/bin/$PROG
exit0
fi
if["$1"=="uninstall"];then
echo"Uninstalling..."
rm/usr/bin/$PROG
exit0
fi
if["$1"=="mkalias"];then
forsain${SEARCH_ALIAS[@]};do
echoalias$sa="/usr/bin/$PROG$sa"
done
exit0
fi
QUERY_SHORTCUT=$1
#echo$##debug
if[$#-gt1];then
shift
QUERY_PARA=$*
#echo$QUERY_PARA#debug
fi
count=0
forsain${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