简体   繁体   English

在bash脚本中使用-s命令

[英]Using -s command in bash script

I have a trivial error that I cant seem to get around. 我有一个琐碎的错误,我似乎无法解决。 Im trying to return the various section numbers of lets say "man" since it resides in all the sections. 我试图返回让我们说“ man”的各个部分的编号,因为它驻留在所有部分中。 I am using the -s command but am having problems. 我正在使用-s命令,但是有问题。 Every time I use it I keep getting "what manual page do you want". 每次使用它,我都会不断收到“您想要什么手册页”。 Any help? 有什么帮助吗?

In the case of getting the section number of a command, you want something like man -k "page_name" | awk -F'-' "/^page_name \\(/ {print $1}" 在获取命令的节号的情况下,您需要类似man -k "page_name" | awk -F'-' "/^page_name \\(/ {print $1}" man -k "page_name" | awk -F'-' "/^page_name \\(/ {print $1}" , replacing any occurrence of page_name with whatever command you're needing. man -k "page_name" | awk -F'-' "/^page_name \\(/ {print $1}" ,用您需要的任何命令替换出现的page_name

This won't work for all systems necessarily as the format for the "man" output is "implementation-defined". 这不一定对所有系统都有效,因为“ man”输出的格式是“实现定义的”。 In other words, the format on FreeBSD, OS X, various flavours of Linux, etc. may not be the same. 换句话说,FreeBSD,OS X,Linux的各种版本等上的格式可能不相同。 For example, mine is: 例如,我的是:

page_name (1)               - description

If you want the section number only, I'm sure there is something you can do such as saving the result of that line in a shell variable and use parameter expansion to remove the parentheses around the section number: 如果您只需要节号,我敢肯定您可以做一些事情,例如将该行的结果保存在shell变量中,并使用参数扩展来删除节号周围的括号:

man -k "page_name" | awk -F'-' "/^page_name \(/ {print $1}" | while IFS= read sect ; do
    sect="${sect##*[(]}"
    sect="${sect%[)]*}"
    printf '%s\n' "$sect"
done

To get the number of sections a command appears in, add | wc -l 要获取命令所在的节数,请添加| wc -l | wc -l at the end on the same line as the done keyword. | wc -ldone关键字位于同一行的末尾。 For the mount command, I have 3: 对于mount命令,我有3个:

2
2freebsd
8

The correct syntax requires an argument. 正确的语法需要一个参数。 Typically you're looking for either 通常,您正在寻找

man -s 1 man

to read the documentation for the man(1) command, or 阅读有关man(1)命令的文档,或

man -s 7 man

to read about the man(7) macro package. 了解有关man(7)宏程序包的信息。

If you want a list of standard sections, the former contains that. 如果要列出标准部分,则前者包含该部分。 You may have additional sections installed locally, though. 不过,您可能在本地安装了其他部分。 A directory listing of /usr/local/share/man might reveal additional sections, for example. 例如,目录/usr/local/share/man可能会显示其他部分。

(Incidentally, -s is not a "command" in this context, it's an option.) (顺便说一句,在这种情况下, -s不是“命令”,而是一个选项。)

You've misinterpreted the nature of -s . 您误解了-s的本质。 From man man : man man

-S list, -s list, --sections=list -S列表,-s列表,-sections =列表

List is a colon- or comma-separated list of `order specific' manual sections to search. List是要搜索的“特定于订单”手册部分的冒号或逗号分隔列表。 This option overrides the $MANSECT environment variable. 此选项将覆盖$ MANSECT环境变量。 (The -s spelling is for compatibility with System V.) (-s的拼写是为了与System V兼容。)

So when man sees man -s man it thinks you want to look for a page in section "man" (which most likely doesn't exist, since it is not a normal section), but you didn't say what page, so it asks: 因此,当男人看到man -s man它认为您想在“ man”部分中查找页面(该页面很可能不存在,因为它不是正常的部分),但是您没有说出什么页面,所以它要求:

What manual page do you want? 您想要什么手册页?

BTW, wrt "man is just the test case cuz i believe its in all the sections" -- nope, it is probably only in one, and AFAIK there isn't any word with a page in all sections. 顺便说一句,wrt “人只是测试用例,因为我相信它在所有部分中都是这样” -不,它可能只在一个部分中,而AFAIK在所有部分中都没有任何单词。 More than 2 or 3 would be very unusual. 超过2或3将是非常不寻常的。

The various standard sections are described in man man too. 各种标准部分也在man man进行了描述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM