[Unix]根据man生成所有命令的说明文档

一段shell脚本,放在linux中运行,会自动枚举/bin、/usr/bin等目录下的所有可执行文件,然后查找man生成html的说明文档。

生成的文档包中index.html是目录。

这包文档可以用在无man而又想使用unix tools的时候,如在windows下玩grep。

#! /bin/bash

helpDir=man_pages
main_file=./$helpDir/index.html
cmds=`
{
for j in ${PATH//:/ }
do
ls $j
done
} | sort | uniq `


rm -f -r $helpDir
mkdir $helpDir

echo "<head> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /> " >> $main_file
echo "<table border=\"1\"><tr><th>命令</th><th>描述</th></tr>" >> $main_file

for i in $cmds
do
echo "processing \"$i\"..."

file=./$helpDir/${i}.html

echo "<head> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /> " >> $file
echo "<p><a href="index.html">返回目录</a></p>" >> $file
echo "<hr>" >> $file
echo "<pre>" >> $file

man $i >> $file 2>/dev/null &&
{
echo "</pre>" >> $file

describ=`sed -n -e '/NAME/ {n;p;q}' $file | cut -f 2 -d '-'`

echo "<tr>" >> $main_file
echo "<td><a href=\"$i.html\">$i</a></td>" >> $main_file
echo "<td>$describ</td>" >> $main_file
echo "</tr>" >> $main_file
} ||
rm $file

done

echo "</table>" >> $main_file

仅46行啊,感慨一下shell脚本的信息密度之高!46行的c语言还在hello world呢。

posted @ 2012-01-02 01:08  Scan.  阅读(869)  评论(5编辑  收藏  举报