我经常发现自己man
输入命令只是为了了解一个特定的选项。大多数时候我都可以很好地搜索到该选项,除非是像ffmpeg
或gcc
我必须逐步搜索大约 40 个匹配项才能找到该选项的实际描述...
有时我会很幸运,搜索“选项”这个词来接近答案,然后从那里进行细化,但如果我能够可靠地直接跳转到相关选项,那就太好了。如果有一个工具可以解析选项并构建一个可以进行搜索的数据库,那就太酷了,但在查看了几页 groff 标记后,我确定这只是一个最佳猜测,因为 groff 标记中缺乏元信息……在我理想的世界中女人模式在 emacs 中将支持搜索特定选项...:)
有什么技巧可以直接跳转到手册页中的特定选项吗?
答案1
下面是我的脚本。它叫做他。
#!/bin/sh
# he - print brief help about a single option or command
# Mikel Ward <[email protected]>
# Example Usage:
# he bash continue
# he rsync -v
scriptname=he
usage()
{
cat <<EOF 1>&2
Usage: $scriptname <command> [<option|section>]
Example:
$scriptname bash getopts (shows documentation for bash getopts)
$scriptname ssh -v (shows documentation for ssh -v flag)
$scriptname select (shows SYNOPSIS for select(2))
$scriptname 'open(2)' (shows SYNOPSIS for open(2))
EOF
}
if test $# -lt 1; then
usage
exit 2
fi
manpage="$1"
# show the SYNOPSIS section if no section or option was given
option="${2:-SYNOPSIS}"
# handle manpage(number)
case $manpage in *\(*\))
page=${manpage%\(*\)}
section=${manpage#$page}
section=${section#\(}
section=${section%\)}
manpage=$page
;;
esac
man ${section:+-s $section} "$manpage" | perl -n -e '
BEGIN {
$option = "'$option'";
$inside_option = 0;
}
if (!$inside_option) {
if (/^(\s*)\Q$option\E\b/p) {
# start of this option
$option_indentation = $1;
$inside_option = 1;
$saw_blank_line = 0;
print;
}
} else {
if (/^$/) {
$saw_blank_line = 1;
print;
} elsif (/^\Q$option_indentation\E\S/ and $saw_blank_line) {
# item at same indentation => start of next option
$inside_option = 0;
} elsif (/^\S/) {
# new heading => start of next section
$inside_option = 0;
} else {
print;
}
}
'
$ he cp
SYNOPSIS
cp [OPTION]... [-T] SOURCE DEST
cp [OPTION]... SOURCE... DIRECTORY
cp [OPTION]... -t DIRECTORY SOURCE...
$ he gcc -dD
-dD Dump all macro definitions, at the end of preprocessing, in addition to normal output.
$ he rsync -v
-v, --verbose increase verbosity
$ he bash getopts
getopts optstring name [args]
getopts is used by shell procedures to parse positional parameters. optstring contains the option characters to be recognized; if a character is followed by a colon, the option is
expected to have an argument, which should be separated from it by white space. The colon and question mark characters may not be used as option characters. Each time it is invoked,
getopts places the next option in the shell variable name, initializing name if it does not exist, and the index of the next argument to be processed into the variable OPTIND. OPTIND is
initialized to 1 each time the shell or a shell script is invoked. When an option requires an argument, getopts places that argument into the variable OPTARG. The shell does not reset
OPTIND automatically; it must be manually reset between multiple calls to getopts within the same shell invocation if a new set of parameters is to be used.
When the end of options is encountered, getopts exits with a return value greater than zero. OPTIND is set to the index of the first non-option argument, and name is set to ?.
getopts normally parses the positional parameters, but if more arguments are given in args, getopts parses those instead.
getopts can report errors in two ways. If the first character of optstring is a colon, silent error reporting is used. In normal operation diagnostic messages are printed when invalid
options or missing option arguments are encountered. If the variable OPTERR is set to 0, no error messages will be displayed, even if the first character of optstring is not a colon.
If an invalid option is seen, getopts places ? into name and, if not silent, prints an error message and unsets OPTARG. If getopts is silent, the option character found is placed in
OPTARG and no diagnostic message is printed.
If a required argument is not found, and getopts is not silent, a question mark (?) is placed in name, OPTARG is unset, and a diagnostic message is printed. If getopts is silent, then a
colon (:) is placed in name and OPTARG is set to the option character found.
getopts returns true if an option, specified or unspecified, is found. It returns false if the end of options is encountered or an error occurs.
但是如果您无法访问这样的脚本,只需运行less
,然后输入/^ *-option
(注意空格),例如,在gcc
手册页中,输入/^ *-dD
Enter以查找该选项的文档-dD
。
这是可行的,因为该选项通常出现在行的开头。
答案2
如果您想查找 1 个字母的代码,跳转到特定选项的最简单方法是:输入 3 个空格,然后使用“/”手册搜索功能输入该选项。
例如,假设我们要跳转到 grep 手册的 -p 选项。
搜索字符串:
/ -p
匹配
-p If -R is specified, no symbolic links are followed. This is the
default.
答案3
这是我使用的功能。我将其称为“mans”,即“man search”。
mans ()
{
local pages string;
if [[ -n $2 ]]; then
pages=(${@:2});
string="$1";
else
pages=$1;
fi;
man ${2:+--pager="less -p \"$string\" -G"} ${pages[@]}
}
用法:
$ mans ^OPTIONS grep find xargs
答案4
这是我用来显示选项描述和命令描述的函数。
#!/usr/bin/env bash
usage() {
cat <<EOF
Name: manop
Description: This script outputs a command description or an option
description from the man page.
Usage:
manop wget -b : Show wget -b option description
manop ls: Show the ls command description
EOF
exit 2
}
if [ $# -eq 0 ]; then
usage
fi
if [ $# -eq 2 ]; then
man "$1" | col -bx | sed -n "/^ *$2/,/^$/p"
else
man "$1" | col -bx | sed -n "/^DESCRIPTION/,/^$/p"
fi
exit 0
如何使用它:
$ manop ls -h
-h, --human-readable
with -l and -s, print sizes like 1K 234M 2G etc.
$ manop col
DESCRIPTION
The col utility filters out reverse (and half reverse) line feeds so that
the output is in the correct order with only forward and half forward line feeds,
and replaces white-space characters with tabs where possible. This can be useful
in processing the output of nroff(1) and tbl(1).