用于具有自定义输出的man的bash脚本

用于具有自定义输出的man的bash脚本,bash,manpage,Bash,Manpage,我正在尝试编写一个bash脚本,它只为“n”命令提供第一行man 例如: $ sh ./start.sh ls wazup top ls - list directory contents wazup - manpage does not exist top - display Linux tasks 这是我当前的代码: ! bin/bash/ while [ -n "$1" ] do which $1> /dev/null man $1 | head -6 | tail -1 if

我正在尝试编写一个bash脚本,它只为“n”命令提供第一行man

例如:

$ sh ./start.sh ls wazup top 
ls - list directory contents
wazup - manpage does not exist
top - display Linux tasks
这是我当前的代码:

! bin/bash/
while [ -n "$1" ]
do
which $1> /dev/null
man $1 | head -6 | tail -1
if [ $? = 0  ]
then
echo "manpage does not exist"
fi
shift
done
我的输出是:

ls - list directory contents
manpage does not exist
No manual entry for wazzup
manpage does not exist
top - display Linux processes
manpage does not exist

检查由
man
返回的状态代码,不要在通过
head
tail
(这将是错误的,因为这将是
tail
的返回状态)。

非常感谢Alex

通过在您的帮助下不使用管道解决了此问题!:)

以下是我为需要它的人编写的最终代码:

#!/bin/bash
while [ -n "$1" ]
do
which $1> /dev/null
if [ $? = 0 ]
then
man -f $1
else 
echo "$1: manpage does not exist" 
fi
shift
done

退出状态为0表示成功(与C样式布尔约定相反)。非零值表示故障。