GNU/Linux >> Linux の 問題 >  >> Linux

man ページで特定のオプションを検索する

これが私のスクリプトです。彼と呼ばれています。

#!/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 man ページ、タイプ /^ *-dD 入る -dD のドキュメントを見つける オプション。

オプションは通常、行の先頭に表示されるため、これは機能します。


これは私が使用する機能です。 「男探し」の意味で「mans」と呼んでいます。

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

Linux
  1. マニュアルページの数字はどういう意味ですか?

  2. ロングマンページを読んで検索しますか?

  3. man ページでフラグを探す方法はありますか?

  1. 独自の動力工具のマニュアルページエントリを追加するにはどうすればよいですか?

  2. Getoptsを使用して1つのオプションに2つの引数を提供しますか?

  3. 特定のModSecurityルールのIPをホワイトリストに登録

  1. Linux で特定のユーザーに対して特定のコマンドを無効にする方法

  2. Linux で特定のファイル タイプを再帰的に grep する

  3. cd コマンドの man ページがない