[Wolves] Linux man pages

chris procter chris-procter at talk21.com
Thu Nov 15 11:17:31 GMT 2007


> Incidentally, I'm in work at the moment and having browsed some
> websites for shell tutorials I've come up with the following script to
> do it. Can anyone see any mistakes (probably loads!) or give any
> hints.  I assume the part where I run the command and pass it to a
> text file is wrong but couldn't find much on that!
> 
> {CODE}

This isn't the way I'd have done this but here goes

> #!/bin/bash
> #
> #  ManPage Extractor
> #  Chris Maggs 2007
> #  Released as GPL 3
> #
> clear
> echo "ManPage Extractor 0.1"
> echo ""

you could combine these two into 
echo -e "ManPage Extractor 0.1\n"

> #
> #
> # Declare the array of commands - just a few examples
> #
> declare cmd[5]=(grep ls ps pwd umount)

This is a syntax error.
declare -a cmd[5]
declares a 5 element array which is nice but you dont actaully need to declare stuff in shell scripts, instead you can just use

cmd=(grep ls ps pwd umount)

which creates an array and populates it
 
> #
> # Print the man commands to text files
> # Tell user what is happening first and warn that it
> # may take a while as there are loads of commands!
> #
> echo "Creating text files. This may take a while!"
> #
> # Now the juicy bits!
> #
> for (( i = 0; i <= 4 ; i++ ))
> do
>   man $i > $i.txt
> done

If you expand out this for loop it runs:-



man 0 > 0.txt
man 1 > 1.txt

man 2 > 2.txt

man 3 > 3.txt

man 4 > 4.txt

which isn't what you want to do, instead of $i you need ${cmd[$i]}  to get the ith element of the array

also you've hardwired the number of iterations whereas ${#cmd} gives the length of the array so I'd do the loop as:-
 
for (( i = 0; i <= ${#cmd} ; i++ ))
do
    echo "creating ${cmd[$i]}.txt"
    man ${cmd[$i]} > ${cmd[$i]}.txt
done

so then you can add new commands just by adding them to the cmd=(...) line above

> #
> # Tell the user we've finished!
> #
> echo -e "Woohoo! Finished :)"

You dont need the -e here but now I'm just being picky  :)


So the full script (with comments removed for brevity) is:-


#!/bin/bash
#
#  ManPage Extractor
#  Chris Maggs 2007
#  Released as GPL 3
#
clear
echo -e "ManPage Extractor 0.1\n"

cmd=(grep ls ps pwd umount)

echo "Creating text files. This may take a while!"

for (( i = 0; i <= ${#cmd} ; i++ ))

do

    echo "creating ${cmd[$i]}.txt"

    man ${cmd[$i]} > ${cmd[$i]}.txt

done

echo -e "Woohoo! Finished :)"


chris





      ___________________________________________________________ 
Want ideas for reducing your carbon footprint? Visit Yahoo! For Good  http://uk.promotions.yahoo.com/forgood/environment.html



More information about the Wolves mailing list