I\'m able to get the cursor position with getpos(), but I want to retrieve the selected text within a line, that is \'<,\'>.
On Linux, there is a cheap but effective alternative to programming such a GetVisualSelection() function yourself: use the * register!
The * register contains the content of the most recent Visual selection. See :h x11-selection.
In your script you could then simply access @* to get the Visual selection.
let v = @*
Incidentally, * is also a neat little helper in interactive use. For example, in insert mode you can use CTRL-R * to insert what you had selected earlier. No explicit yanking involved.
This works only on operating systems that support the X11 selection mechanism.