我正在尝试编写一个脚本来将手册页转换为 PDF 文件。我现在的脚本是:
#! /usr/bin/env bash
[[ $# -ne 1 ]] && { echo "Usage: $(basename $0) [command]" ; exit 1 ; }
man -t ${1} | ps2pdf14 - > "${1}_man.pdf"
问题是,如果手册页不存在,脚本仍会继续执行,生成一个空的 PDF 文件。所以我想知道是否有一种方法可以确定手册页是否存在?
最佳答案
使用函数的 Man 退出状态
man(1) 手册页定义了以下退出状态代码:
EXIT STATUS
0 Successful program execution. 1 Usage, syntax or configuration file error. 2 Operational error. 3 A child process returned a non-zero exit status. 16 At least one of the pages/files/keywords didn't exist or wasn't matched.
这意味着您可以使用 man 本身的退出状态来确定页面是否可以通过 manpath 访问。例如:
check_for_man_page () {
man "$1" > /dev/null 2>&1
}
有了这个函数,你可以像这样在退出状态上使用测试条件:
$ check_for_man_page "cat" && echo 'Found it!'
Found it!
$ check_for_man_page "quux" || echo 'Not found!'
Not found!
使用 If/Else 语句
这是执行此操作的另一种方法,使用 if/else 语句来确定是否运行您的原始代码:
if man "$1" > /dev/null 2>&1
then
man -t ${1} | ps2pdf14 - > "${1}_man.pdf"
else
echo "Missing man page: $1" >&2
fi
关于linux - 如何检查手册页是否存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12241709/