UraniumSnake
UraniumSnake

Reputation: 465

bash script for man with custom output

I'm trying to do a bash script which gives me only the first line of man for "n" commands.

example:

$ sh ./start.sh ls wazup top 
ls - list directory contents
wazup - manpage does not exist
top - display Linux tasks

This is my current code:

! 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

My Output is:

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

Upvotes: 3

Views: 149

Answers (2)

UraniumSnake
UraniumSnake

Reputation: 465

Many Thanks Alex!

Solved it by not using pipes with your help! :)

Here's my final code for anyone that needs it:

#!/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

Upvotes: 1

alex
alex

Reputation: 490263

Check the status code returned by man, not once it's piped through head and tail (which will be wrong as it will be the return status of tail).

Upvotes: 2

Related Questions