shell script to ease access to the bash manual from the Terminal

Hello. :slight_smile:

As we do shell scripts, or other scripts, involving bash commands, it is sometimes necessary to look up in the manual.

This is kind of tricky at times. There is the apropos command that also can be executed from the command line, that can sometimes give you a hint for finding the commands.

At other times, not knowing exactly what you look for, you can search for synonyms in the bash manual, like you would by google, when you are not totally sure of what you are looking for.

The man command is quite versatile, hit “h” to get its options.

The output you get, when searching by “/” can be quite elaborate. Typing in regexps can also be feeling a bit enneerving.

I have brushed the dust of a shell script I wrote many years ago, to look up bash keywords quickly.

Save it as bashcmd, and chmod it to u+x aka: chmod u+x bashcmd.

Then you can try “bashcmd for” from the command line, and move between the hits by pressing “n” and “p” respectively.

[code]#! /bin/bash

Brings us hopefully fast to whatever searchword we

wants to look up in the bash manual

if [ $# -ne 1 ] ; then
echo “Takes exactly one parameter.”
fi
man bash |/usr/bin/less -p "^[ ][ ]*$1 "[/code]

Hello.

This one lets you move quickly between the main sections in bash.1 (the bash manual) by pressing “n” and “p” respectively.

I have put in in my bin folder after changing it into an executable with the name “sections”.

The i is there to turn off case sensitivity!

#!/bin/bash man bash |less -i -p "^[A-Z][A-Z]+"

I’m using on the command line:

man -t bash | open -f -a preview

The manual of bash (change to whatever manual you want) will be outputted in postscript formatted and it can be opened/streamed into preview. So you will see the manual as an PDF

edit: Wrapped in a small script:

set theCommand to text returned of (display dialog "Which manual do you want to open in Preview?" default answer "bash")

if length of theCommand < 1 then return

--quoted form is only to make inserted text injection-safe
do shell script "man -t " & quoted form of theCommand & " | open -f -a preview"

I’m using an online version.
The advantage is to be able to click on the reference links to other pages


set theCommand to text returned of (display dialog "Which manual do you want to open?" default answer "bash")
open location createManPage(theCommand)


on createManPage(topic)
    set section to word 2 of (do shell script "man " & topic)
    return "http://www.manpagez.com/man/" & section & "/" & topic & "/"
end createManPage


The drawback is that online manual pages are very general and doesn’t match with heavily modded command line utilities by Apple like the open command. Maybe we can add a chooser :slight_smile:

tell (display dialog "Which manual do you want to open?" default answer "bash" buttons {"Cancel", "Online", "Local"} default button 3) to set {theCommand, theLocation} to {text returned, button returned}

if theLocation is "Online" then
	openOnline(theCommand)
else
	openLocal(theCommand)
end if

on openOnline(topic)
	set section to word 2 of (do shell script "man " & topic)
	open location "http://www.manpagez.com/man/" & section & "/" & topic & "/"
end openOnline

on openLocal(topic)
	--quoted form is only to make inserted text injection-safe
	do shell script "man -t " & quoted form of topic & " | open -f -a preview"
end openLocal

Hello.

The point of my second shell script, and the first, is to be able to move back and forth quickly by utilizing the keypresses “n” and not “p” but shift “n” in less. To get to the material fast.

I love the open command! LOVE it! :smiley:

But for manual pages, if I am not to get at it that quickly, I’d prefer to look up the manpages in XCode, then I guesss I’d have the same cross reference possibiltiy that StephanK describes. This is at least possible with XCode 3.2 until I upgrade.

(I really prefer to read books, (Like “The Unix Programming Environment”, and “The System V Environment” and look up stuff when I need to implement something, by reading the manual pages as I go along.

The apropos command is great at times, when I don’t know exactly which command it is that gives me the functionality I want, (or I have forgotten it’s name. :slight_smile: )