Getting the Selection w/in Unix

I’m trying to get the current selection, as text, as a unix command. I presumed it’d be fairly easily done as an applescript somehow, but I haven’t succeeded in doing so, alas!
So basically I’d like to, from the terminal, run a simple program like “getselection” and have it return the text of the current selection. (For extra credit: if there is no (primary) selection, then return the clipboard as text)
Is this at all possible in applescript? I may be looking at it the wrong way … I’m a unix/linux programmer … although I did work on the initial Mac (did the printing) … but its been a while!

I would like to know this too.