LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 863|回复: 1

mank.sh - apropos 或 man -k 查询结果浏览器

[复制链接]
发表于 2008-4-29 17:40:54 | 显示全部楼层 |阅读模式
用 dialog 做的 apropos/man 界面,查找后列出结果,选择条目后
调用 man 查看,如果有 perldoc 的 man pages,那么可以拿这个
当作简单的 Perl 手册查看器了。

使用办法: 运行 ./mank.sh,支持参数 -s <section>。


  1. #!/bin/bash
  2. #
  3. # A manual viewer for UNIX-like systems.
  4. #
  5. # Requirements:
  6. #   bash, dialog or whiptail, apropos, mandb
  7. #   (Usually they have been installed on your system.)
  8. #
  9. # Usage:
  10. #   ./mank                      # let mank select which tool
  11. #   DIALOG=whiptail ./mank      # prefer whiptail
  12. #   ./mank -s 2                 # only search in section 2.
  13. #
  14. # Author:
  15. #   Liu Yubao <yubao.liu@gmail.com>
  16. #
  17. # Licence:
  18. #   GPLv3.
  19. #
  20. # ChangeLog:
  21. #   2008-04-29    Liu Yubao
  22. #       * initial version v0.1
  23. #

  24. : ${DIALOG:=dialog}

  25. {
  26.     which $DIALOG || {
  27.         DIALOG=dialog
  28.         which $DIALOG || {
  29.             DIALOG=whiptail
  30.             which $DIALOG || DIALOG=""
  31.         }
  32.     }
  33. } >/dev/null 2>&1

  34. if [ -z "$DIALOG" ]; then
  35.     echo Please install dialog or whiptail first. >&2
  36.     exit 1
  37. fi

  38. which apropos >/dev/null 2>&1 || {
  39.     echo "Can't find \`apropos\` command, will switch to \`man -k\`." >&2
  40.     APROPOS_CMD="man -k"
  41. }


  42. while [ -n "$1" ]; do
  43.     if [ "-s" = $1 ]; then
  44.         SECTION=$2
  45.         break
  46.     fi
  47.     shift
  48. done
  49. [ -z "$APROPOS_CMD" ] && {
  50.     [ -z "$SECTION" ] && APROPOS_CMD="apropos" ||
  51.         APROPOS_CMD="apropos -s $SECTION"
  52. }


  53. tempfile=`tempfile 2>/dev/null` || tempfile=/tmp/mank$$
  54. trap "rm -f $tempfile" 0 1 2 5 15

  55. shopt -s -q extglob


  56. while $DIALOG --inputbox "$APROPOS_CMD" 16 51 2>$tempfile ; do
  57.     $APROPOS_CMD `cat $tempfile` >$tempfile 2>/dev/null

  58.     if [ ! -s $tempfile ]; then
  59.         $DIALOG --msgbox "nothing appropriate." 16 51
  60.         continue
  61.     fi

  62.     if grep ": nothing appropriate." $tempfile >/dev/null; then
  63.         $DIALOG --msgbox "`cat $tempfile`" 16 51
  64.     else
  65.         n=0
  66.         while read item; do
  67.             items[$((n++))]=${item%%+( )-*}
  68.             items[$((n++))]=${item#*+( )- }
  69.         done < $tempfile

  70.         item=${items[0]}
  71.         while $DIALOG --default-item "$tag"    \
  72.                 --menu "Select an item to see its manual."  \
  73.                 0 0 0 "${items[@]}" 2>$tempfile ; do
  74.             item=`cat $tempfile`
  75.             page=${item%% *}
  76.             section=${item#*\(}
  77.             section=${section%)*}
  78.             man "$section" "$page"
  79.         done
  80.     fi
  81. done

复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
发表于 2008-5-2 13:33:14 | 显示全部楼层
不错, 支持!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表