查询 Linux 手册页中的某些标志

查询 Linux 手册页中的某些标志

我经常打开某个 CLI 工具的手册页只是为了检查特定的标志,例如man iptables检查-t标志。

有没有什么工具可以简化它?我当然可以在 Bash 中编写一个简单的函数来 grep 手册页的内容,但我正在寻找使用手册页结构来准确找到我想要的内容(即某个标志的描述)的东西。

答案1

假设您的手册页程序是,您可以使用环境变量预先less传递任何命令。lessLESS

因此,要搜索以下-t选项man iptables

LESS='+/-t' man iptables

/-t这与在 内运行具有相同的效果man ipatbles。您可以更改模式以进行更精细的控制。

如果需要,您可以创建一个函数以便于访问:

search_man () { LESS=+/"$2" man "$1" ;}

现在正在做:

search_man iptables '-t'              

将会有同样的效果。


编辑:

如果您想转到手册页的特定选项而不是搜索,可以使用正则表达式匹配LESS

LESS='+/^[[:blank:]]+-t' man iptables

将直接带您-t进入 的选项说明man iptables。你也可以同样定义一个函数:

search_man () { LESS=+/^[[:blank:]]+"$2" man "$1" ;}

答案2

我还没有找到用于查询特定标志的手册页的 API/机制。然而,这个简单的功能似乎正是我需要的:

function manswitch () { man $1 | less -p "^ +$2" }

用法:

manswitch iptables -t

答案3

有一件事。现在。这个功能:

flag() {
    man "$1" | grep -- "$2";
}

它的工作原理如下:

$ flag iptables -t
iptables [-t table] {-A|-C|-D} chain rule-specification
ip6tables [-t table] {-A|-C|-D} chain rule-specification
iptables [-t table] -I chain [rulenum] rule-specification
iptables [-t table] -R chain rulenum rule-specification
iptables [-t table] -D chain rulenum
iptables [-t table] -S [chain [rulenum]]
iptables [-t table] {-F|-L|-Z} [chain [rulenum]] [options...]
iptables [-t table] -N chain
iptables [-t table] -X [chain]
iptables [-t table] -P chain target
iptables [-t table] -E old-chain-name new-chain-name
target = -j targetname [per-target-options]
-t, --table table
           This  is  the  default table (if no -t option is passed). It
        iptables -t nat -n -L

好吧,最后两行被破坏了。

无论如何,您知道如何将其添加到您的.bashrc吗?或者您更喜欢将其作为脚本而不是您的脚本~/bin

1.1版

flag() {
    man "$1" | grep -A5 -- "$2";
}

$ flag iptables -t
       iptables [-t table] {-A|-C|-D} chain rule-specification

       ip6tables [-t table] {-A|-C|-D} chain rule-specification

       iptables [-t table] -I chain [rulenum] rule-specification

       iptables [-t table] -R chain rulenum rule-specification

       iptables [-t table] -D chain rulenum

       iptables [-t table] -S [chain [rulenum]]

       iptables [-t table] {-F|-L|-Z} [chain [rulenum]] [options...]

       iptables [-t table] -N chain

       iptables [-t table] -X [chain]

       iptables [-t table] -P chain target

       iptables [-t table] -E old-chain-name new-chain-name

       rule-specification = [matches...] [target]

       match = -m matchname [per-match-options]

       target = -j targetname [per-target-options]

DESCRIPTION
       Iptables  and ip6tables are used to set up, maintain, and inspect the tables of IPv4 and IPv6 packet filter rules in the Linux kernel.  Several different tables may be defined.  Each table contains a
       number of built-in chains and may also contain user-defined chains.

--
       -t, --table table
              This option specifies the packet matching table which the command should operate on.  If the kernel is configured with automatic module loading, an attempt will be made to load the appropriate
              module for that table if it is not already there.

              The tables are as follows:

--
                  This is the default table (if no -t option is passed). It contains the built-in chains INPUT (for packets destined to local sockets), FORWARD (for packets being routed  through  the  box),
                  and OUTPUT (for locally-generated packets).

              nat:
                  This  table is consulted when a packet that creates a new connection is encountered.  It consists of three built-ins: PREROUTING (for altering packets as soon as they come in), OUTPUT (for
                  altering locally-generated packets before routing), and POSTROUTING (for altering packets as they are about to go out).  IPv6 NAT support is available since kernel 3.7.
--
               iptables -t nat -n -L
              Please note that it is often used with the -n option, in order to avoid long reverse DNS lookups.  It is legal to specify the -Z (zero) option as well, in which case the chain(s) will be atom‐
              ically listed and zeroed.  The exact output is affected by the other arguments given. The exact rules are suppressed until you use
               iptables -L -v

       -S, --list-rules [chain]

相关内容