[RFC PATCH v1 1/2] src/bin/sman: Add script
From: Seth McDonald <hidden>
Date: 2026-01-27 09:20:38
Subsystem:
the rest · Maintainer:
Linus Torvalds
Add a new bash script 'sman', which displays only select sections of a given man page. For example, the SYNOPSIS of stat(2), or the STANDARDS and BUGS of printf(3). Similar to mansect(1), except it displays the rendered man page rather than the associated source code. It can parse a formatted man page, displaying the specified sections with bold and italic text if MAN_KEEP_FORMATTING is set. Which makes it more useful than manually isolating the wanted page sections with awk(1) or sed(1), which may remove the formatting. Signed-off-by: Seth McDonald <redacted> --- src/bin/sman | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100755 src/bin/sman
diff --git a/src/bin/sman b/src/bin/sman
new file mode 100755
index 000000000000..8bc239d16e96
--- /dev/null
+++ b/src/bin/sman@@ -0,0 +1,54 @@ +#!/bin/bash +# +# Copyright, the authors of the Linux man-pages project +# SPDX-License-Identifier: GPL-3.0-or-later + +name="$(basename "$0")" + +# fail [error message] +fail() { + (($# >= 1)) && echo "$name: $1" + echo "Usage: $name [man section] <man page> <page section>..." + exit $# +} + +(($# == 0)) && fail +(($# < 2)) && fail "Too few arguments." + +[[ $(which man) ]] || fail "Failed to find man(1)." +[[ $(which sed) ]] || fail "Failed to find sed(1)." + +[[ $MAN_KEEP_FORMATTING ]] && export MAN_KEEP_FORMATTING=1 +[[ $MANWIDTH ]] && export MANWIDTH + +# There are currently no man pages whose name starts with a digit. So +# its fair to assume that if the first arg starts with a digit, it's +# referring to a man section. +if [[ $1 = [0-9]* ]] +then + msect="$1" + shift 1 +fi + +mpage="$1" +shift 1 +(($# == 0)) && fail "No page sections specified." + +# Check man page exists before getting the same "No manual Entry" error +# for each user-specified page section. +man -w $msect "$mpage" > /dev/null || fail "Failed to find $mpage." + +for psect +do + # If MAN_KEEP_FORMATTING is set, the section headers should be + # in bold. So wrap regex in the corresponding ANSI escape codes + # in this case. + man $msect "$mpage" | + if [[ $MAN_KEEP_FORMATTING ]] + then + sed -En $'/^\e\[1m'"${psect^^}"$'\e\[0?m$/,/^\e\[1m[A-Z][A-Z ]*\e\[0?m$/p' + else + sed -En '/^'"${psect^^}"'$/,/^[A-Z][A-Z ]*$/p' + fi | + sed '$d' # Remove trailing section headers. +done
--
2.47.3
Attachments
- signature.asc [application/pgp-signature] 322 bytes