简体   繁体   English

使用重定向的stdin调用man来显示BASH脚本中的帮助

[英]Calling man with redirected stdin to show help from within a BASH script

Is there any reason why using man with redirected stdin to show help from within a BASH script is a bad idea or not compatible with all (within reason) versions of BASH? 有没有用,为什么任何理由man与重定向标准输入从bash脚本中显示的帮助是一个坏主意或不使用bash的所有(合理范围内)的版本兼容?

The technique works fine with the 2 versions of BASH that I have easy access to: GNU BASH v. 4.3.11 and 4.3.30 (both on Linux). 这个技术适用于我可以轻松访问的两个版本的BASH:GNU BASH v.4.3.11和4.3.30(都在Linux上)。 It seems to me an elegant way to display help for a script, from within the script, when the people who download it are not particularly likely to bother installing the man page, and the man page has already been written. 在我看来,在脚本中显示脚本帮助是一种优雅的方式,当下载它的人不太可能打扰安装手册页时,手册页已经写好了。

Below is a bare-bones example of what I mean. 下面是我的意思的一个简单的例子。 The display_usage_message function would get called when the help option is used on the command line and the script would call exit when the function returns. 当在命令行上使用help选项时,将调用display_usage_message函数,并且当函数返回时脚本将调用exit

#!/bin/bash

display_usage_message()
{
man /dev/stdin <<EOF

.TH SCRIPT 1 "01 Oct 2015" "1.0" "SCRIPT VERSION 1.0"

.SH NAME
Script \- whatever it does.

.SH SYNOPSIS
script options etc.

.SH DESCRIPTION
This is a description which is not very long. :)
EOF
}

display_usage_message
exit

Thanks. 谢谢。

EDIT: In light of chepner's comment, would this slight variation be reliable? 编辑:根据chepner的评论,这种轻微的变化是否可靠?

#!/bin/bash

display_usage_message()
{

temp_man_filename=$(mktemp -q -t "tmp.man.page.XXXXXX")

cat <<EOF >>$temp_man_filename

.TH SCRIPT 1 "01 Oct 2015" "1.0" "SCRIPT VERSION 1.0"

.SH NAME
Script \- whatever it does.

.SH SYNOPSIS
script options etc.

.SH DESCRIPTION
This is a description which is not very long. :)
EOF

man $temp_man_filename

rm $temp_man_filename
}

display_usage_message
exit

The expectation is for the help message to be a short -- ideally, less than a screenful -- reminder of the available functionality, while the manual, conversely, should explain concepts and document error messages, exit codes, operational constraints, etc. 期望是帮助消息是一个简短的 - 理想情况下,不是一个屏幕 - 提醒可用的功能,而手册,相反,应该解释概念和文档错误消息,退出代码,操作约束等。

More fundamentally, keeping some or all of the documentation embedded in, or coupled with, the source code is good practice, actively promoted eg in Knuth's Literate Programming and practiced (perhaps less strictly and formally) by eg Perl POD and Javadoc authors. 更基本的是,将部分或全部文档保存在源代码中或与源代码相结合是良好的实践,例如在Knuth的文字编程中得到积极推动,并且由Perl POD和Javadoc作者实践(可能不那么严格和正式)。

Incidentally, man markup feels somewhat obscure in this day and age. 顺便说一下,在这个时代, man标记感觉有些模糊。 Perhaps you would want to explore POD or Markdown as friendlier, more modern alternatives (though not quite as versatile for advanced use). 也许你会想要探索POD或Markdown作为更友好,更现代的替代方案(虽然不太适合高级用途)。 POD is easy enough to embed in shell scripts, and formatting tools will be installed anywhere you have Perl. POD很容易嵌入shell脚本中,格式化工具将安装在Perl的任何地方。

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

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